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运行它呢?
发布于 2015-02-02 23:43:31
我建议你用ProcessBuilder,你可以用inheritIO()之类的,
ProcessBuilder pb = new ProcessBuilder("python", "setup.py", "py2exe");
pb.inheritIO();
try {
Process p = pb.start();
p.waitFor(); // <-- wait for the process to finish
} catch (Exception e) {
e.printStackTrace();
}https://stackoverflow.com/questions/28288499
复制相似问题