我正在尝试使用java中的运行时运行python脚本,但命令不起作用,我收到此错误:
无法运行程序" python":CreateProcess error = 2,系统找不到指定的文件
甚至我用可执行的python设置PATH变量。
public void call(){
String command= "python C:/Python27/lib2.py";
Process p = Runtime.getRuntime().exec(command);
}
PATH = "C:\Python27"
答案 0 :(得分:0)
看看ProcessBuilder。示例代码显示:
ProcessBuilder pb =
new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
File log = new File("log");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
assert pb.redirectInput() == Redirect.PIPE;
assert pb.redirectOutput().file() == log;
assert p.getInputStream().read() == -1;
你可以env.put("PATH", "c://python/bin");
或者找到python安装的地方,以确保在路径中找到python。