由于Jython项目是非活动的,而不是python 3,所以不是最新的(请参见注释),所以作为系统可执行文件启动python.exe的选项2是好的。
因为runtime.exec不执行dos cli,而是直接执行一个可执行文件,所以它无法按照您对其进行编码的方式工作。所以您启动的是python.exe,而py文件就是这个命令的一个参数
下面是启动它的方法
public static String execPython(String pythonFile) throws java.io.IOException {
// modification here: provide a String array with "python" as first argument,
// you may add other argument of your python program
Process proc = Runtime.getRuntime().exec(new String[] {"python",pythonFile});
java.io.InputStream is = proc.getInputStream();
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
String val = "";
if (s.hasNext()) {
val = s.next();
} else {
val = "";
}
return val;
}
public static void main(String[] args) {
try {
String stdStream = execPython("C:\\Users\\johns\\Desktop\\python\\pytest.py");
System.out.println(stdStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}