Executing a Command from Java and Waiting for the Command to Finish

我与影子孤独终老i 提交于 2019-11-30 08:54:35
Matt R. Johnson

I manged to find the answer elsewhere. To keep the initial process open until the batch file finished all you need is "/wait"

Process p = Runtime.getRuntime().exec("cmd /C start /wait filepath.bat");
int exitVal = p.waitFor();

calling "cmd /c start" causes cmd to fire off another instance and exit immediately. Try taking out the "start" command.

Pedro Arnoldo Machado Duran

The answer given is correct. I added that the window opened by the code needs to be closed manually.

Process p = Runtime.getRuntime().exec("cmd /C start /wait filepath.bat");
int exitVal = p.waitFor();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!