java调用python时报错。
其他问答
1
我在用java调用一个main.py文件时报错,但是在同样的路劲下调用搜狗的.exe文件能运行
public static void main(String[] args) {
Runtime rn = Runtime.getRuntime();
Process p = null;
try {
// PythonInterpreter interpreter = new PythonInterpreter();
// InputStream filepy = new FileInputStream("D:\\demo.py");
// interpreter.execfile(filepy); ///执行python py文件
// filepy.close();
p = rn.exec("e:/python/main.py");
InputStreamReader isr=new InputStreamReader(p.getInputStream(),"gbk");
BufferedReader br=new BufferedReader(isr);
String res;
while((res = br.readLine())!= null){
System.out.println(res);
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
Cannot run program "e:/python/main.py": CreateProcess error=5, ¾ܾø·Ã
at java.lang.ProcessBuilder.start(ProcessBuilder.java:470)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at com.desktop.ipnet.B.main(B.java:21)
-
//接收错误流 isError = new BufferedReader(new InputStreamReader(process.getErrorStream(),"gbk")); sb= new StringBuilder();
使用以上代码获取错误信息,然后再调试。
-
isError = new BufferedReader(new InputStreamReader(process.getErrorStream(),"gbk")); sb= new StringBuilder();
发表回复