【发布时间】:2015-02-02 23:03:41
【问题描述】:
public void run() {
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("C:\\Windows\\System32\\cmd.exe");
stdin = pr.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(stdin));
writer.write("python setup.py py2exe");
writer.close();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我正在尝试通过 Java 在命令行中运行“python setup.py py2exe”行,但是当我运行上面的代码时,它不起作用(没有错误,但代码应该在目录,但它没有)。
如果我将它直接放入命令提示符,该命令将完美运行。
我怎样才能让它通过 Java 运行?
【问题讨论】:
-
你试过直接运行python吗?用 cmd /c python 怎么样?
-
您是否尝试在命令后将换行符写入流?
-
@MadProgrammer 我刚刚尝试使用 `writer.write("cmd /c python") 并且没有任何乐趣。你是这个意思吗?
-
@Xynariz 我刚刚跑了
writer.write("python setup.py py2exe\n"),没有任何改变。还有其他想法吗? -
另请参阅When Runtime.exec() won't,了解有关正确创建和处理流程的许多好技巧。然后忽略它引用
exec并使用ProcessBuilder创建进程。
标签: java python cmd runtime.exec processbuilder