简体   繁体   中英

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

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).

By using start , you are asking cmd.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.

尝试在start命令前添加/wait参数。

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()

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("Batch file done.");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM