Runtime.exec().waitFor() doesn't wait until process is done

匿名 (未验证) 提交于 2019-12-03 01:56:01

问题:

I have this code:

File file = new File(path + "\\RunFromCode.bat"); file.createNewFile();  PrintWriter writer = new PrintWriter(file, "UTF-8"); for (int i = 0; i <= MAX; i++) {     writer.println("@cd " + i);     writer.println(NATIVE SYSTEM COMMANDS);     // more things }  writer.close();  Process p = Runtime.getRuntime().exec("cmd /c start " + path + "\\RunFromCode.bat"); p.waitFor();  file.delete();

What happens is that the file deleted before it actually executed.

Is this because the .bat file contains only native system call? How can I make the deletion after the execution of the .bat file? (I don't know what the output of the .bat file will be, since it dynamically changes).

回答1:

By using start, you are askingcmd.exe to start the batch file in the background:

Process p = Runtime.getRuntime().exec("cmd /c start " + path + "\\RunFromCode.bat");

So, the process which you launch from Java (cmd.exe) returns before the background process is finished.

Remove the start command to run the batch file in the foreground - then, waitFor() will wait for the batch file completion:

Process p = Runtime.getRuntime().exec("cmd /c " + path + "\\RunFromCode.bat");

According to OP, it is important to have the console window available - this can be done by adding the /wait parameter, as suggested by @Noofiz. The following SSCCE worked for me:

public class Command {  public static void main(String[] args) throws java.io.IOException, InterruptedException {        String path = "C:\\Users\\andreas";         Process p = Runtime.getRuntime().exec("cmd /c start /wait " + path + "\\RunFromCode.bat");         System.out.println("Waiting for batch file ...");        p.waitFor();        System.out.println("Batch file done.");    } }

If RunFromCode.bat executes the EXIT command, the command window is automatically closed. Otherwise, the command window remains open until you explicitly exit it with EXIT - the java process is waiting until the window is closed in either case.



回答2:

Try adding /wait parameter in front of the start command.



回答3:

waitForProcessOutput()

Did the trick for us.

See:

http://docs.groovy-lang.org/docs/groovy-1.7.2/html/groovy-jdk/java/lang/Process.html#waitForProcessOutput()

Code Example (used in SOAPUI)

def process = "java -jar ext\\selenese-runner.jar".execute()  process.waitForProcessOutput()  def exitValue = process.exitValue()


回答4:

None of the code described in the commentary mark as answer is a solution.

First Answer

Process p = Runtime.getRuntime().exec("cmd /c start " + path + "\\RunFromCode.bat");

Second Answer

Process p = Runtime.getRuntime().exec("cmd /c " + path + "\\RunFromCode.bat");

Third Answer

public class Command {  public static void main(String[] args) throws java.io.IOException, InterruptedException {        String path = "C:\\Users\\andreas";         Process p = Runtime.getRuntime().exec("cmd /c start /wait " + path + "\\RunFromCode.bat");         System.out.println("Waiting for batch file ...");        p.waitFor();        System.out.println(
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!