首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java运行时环境无结果

java运行时环境无结果
EN

Stack Overflow用户
提问于 2020-07-06 20:21:30
回答 2查看 82关注 0票数 0

我正在尝试在我的java代码中调用python。然而,我发现如果我在python代码中导入numpy,这就是我的java代码。

代码语言:javascript
复制
Process pcs = Runtime.getRuntime().exec(cmd);
String result = null;
    

BufferedInputStream in = new BufferedInputStream(pcs.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));        
System.out.println("\nExecuting python script file now.");
String lineStr = null;
    while ((lineStr = br.readLine()) != null) {
    result = lineStr;
}
br.close();
in.close();
System.out.println("done!");
System.out.println(result);

这是我的python代码:

代码语言:javascript
复制
import sys 
import os
import numpy as np

a = sys.argv[1]
b = sys.argv[2]
print("hello world!")
print("%s * %s = %s"%(a,b,int(a)*int(b)))

如果不包含"import numpy as np“,结果: 10 * 11 = 110

如果包含"import numpy as np“,则结果为空

有什么直观的解释吗?

EN

回答 2

Stack Overflow用户

发布于 2020-07-07 16:52:17

代码语言:javascript
复制
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Hello{
  public static void main(String[] args)throws java.io.IOException{
    Process pcs=Runtime.getRuntime().exec("python test.py 8 5");// in linux or unix use python3 or python
    String result=null;
    BufferedInputStream in = new BufferedInputStream(pcs.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(in));        
    System.out.println("\nExecuting python script file now.");
    String lineStr = null;
    while ((lineStr = br.readLine()) != null) {
      result = lineStr;
    }
    br.close();
    in.close();
    System.out.println("done!");
    System.out.println(result);
  }
 }

java代码

编译:

javac Hello.java

运行:

java Hello

代码语言:javascript
复制
#test.py
import sys 
import os
import numpy as np
a = sys.argv[1]
b = sys.argv[2]
print("hello world!")
print("%s * %s = %s"%(a,b,int(a)*int(b)))
票数 0
EN

Stack Overflow用户

发布于 2020-07-08 20:19:33

您的应用程序中是否有正确的PYTHONPATH设置?当你的代码中有import numpy as np的时候,你可能会收到一个空的stdout值和一个STDERR格式的ModuleNotFoundError。您可以通过提取STDERR来确认-或者使用以下代码进行检查:

代码语言:javascript
复制
Launch.exe(cmd);

其中,Launch.java是:

代码语言:javascript
复制
public class Launch
{
    /** Launch using FILE redirects */
    public static int exec(String[] cmd) throws InterruptedException, IOException
    {
        System.out.println("exec "+Arrays.toString(cmd));

        Path tmpdir = Path.of(System.getProperty("java.io.tmpdir"));
        ProcessBuilder pb = new ProcessBuilder(cmd);

        Path out = tmpdir.resolve(cmd[0]+"-stdout.log");
        Path err = tmpdir.resolve(cmd[0]+"-stderr.log");
        pb.redirectError(out.toFile());
        pb.redirectOutput(err.toFile());

        Process p = pb.start();
        int rc = p.waitFor();

        System.out.println("Exit "+rc +' '+(rc == 0 ? "OK":"**** ERROR ****")
                          +" STDOUT \""+Files.readString(out)+'"'
                          +" STDERR \""+Files.readString(err)+'"');
        System.out.println();
        return rc;
    }
}

使用numpy的修复方法应该是在调用start()之前访问ProcessBuilder pb.environment()并设置子进程的PYTHONPATH

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62755980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档