英文:
How do I get the information of a batch script started in java, whether it has already ended
问题
我在Java中使用ProcessBuilder调用多个批处理文件,如下所示:
ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", "Start", "batchTest.bat");
File dir = new File(path);
processBuilder.directory(dir);
try {
Process p = processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
现在我想在Java中获取脚本完成的信息,就像在脚本完全完成时进行sysout一样。process.isAlive()
在脚本完成之前变为false。而且process.waitFor()
也不能完成任务。也许有人有一个想法或一个解决方法?
英文:
im calling multiple batch files in Java with the ProcessBuilder like
ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", "Start", "batchTest.bat");
File dir = new File(path);
processBuilder.directory(dir);
try {
Process p = pb.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Now i want the information that the script is done in java. Like just a sysout when the script is completly finished. process.isAlive()
turns false before the script is done. And also process.witFor()
doesnt get the job done. Maybe someone has an idea or a workaround?
答案1
得分: 0
我明白了。process.waitFor()
只有在将参数 "/wait"
添加到 ProcessBuilder 的构造函数中时才有效。
new ProcessBuilder("cmd", "/c", "Start", "/wait", "batchTest.bat");
英文:
Okay, i got it. process.waitFor()
only works if you add the parameter "/wait"
to the Constructor of the ProcessBuilder.
new ProcessBuilder("cmd", "/c", "Start", "/wait", "batchTest.bat");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论