如何获取在Java中启动的批处理脚本的信息,以确定它是否已经结束

huangapple go评论80阅读模式
英文:

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");

huangapple
  • 本文由 发表于 2020年7月22日 17:32:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63031138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定