我正在尝试编写一个从命令提示符编译另一个 java 文件的程序。但是,我遇到了问题。此时,它正在成功执行编译 Mocha.java 的第一部分。但是,我希望它也执行该文件并显示它的输出。它什么也不显示。有什么建议么?
pb = new ProcessBuilder("javac","Mocha.java");
try {
Process shell = pb.start();
OutputStream shellOut = shell.getOutputStream();
shellOut.write("java Mocha".getBytes());
shellOut.close();
InputStream shellIn = shell.getInputStream();
String response = IOUtils.toString(shellIn, "UTF-8");
System.out.println(response);
shellIn.close();
shell.destroy();
} catch (IOException ex) {
System.out.println("failed");
}
笔记:
我也试图让所有的论点都像这样:
pb = new ProcessBuilder("javac","Mocha.java","&&","java","Mocha");
但这不仅不起作用,甚至没有像上面那样编译 Mocha.java。
谢谢!
编辑:
所以我改变了这个来做两个过程。现在工作得很好,伙计们!对于任何有兴趣的人:
pb = new ProcessBuilder("javac","Mocha.java");
try {
Process shell = pb.start();
int error = shell.waitFor();
shell.destroy();
if (error == 0)
{
pb = new ProcessBuilder("java","Mocha");
shell = pb.start();
InputStream shellIn = shell.getInputStream();
String response = IOUtils.toString(shellIn, "UTF-8");
System.out.println(response);
shellIn.close();
shell.destroy();
}
} catch (IOException ex) {
System.out.println("failed");
} catch (InterruptedException ex) {
}