exec().waitFor()不等待进程完成。

时间:2022-08-23 19:34:36

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

这是因为.bat文件只包含本机系统调用吗?如何在.bat文件执行后删除?(我不知道.bat文件的输出会是什么,因为它会动态变化)。

4 个解决方案

#1


35  

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

通过使用start,您是askingcmd。exe在后台启动批处理文件:

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.

因此,在后台进程完成之前,您从Java (cmd.exe)返回的进程将返回。

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

删除start命令,以便在前台运行批处理文件——然后,waitFor()将等待批处理文件完成:

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:

根据OP,有控制台窗口是很重要的——这可以通过添加/等待参数来完成,如@Noofiz所建议的那样。以下SSCCE为我工作:

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.

如果RunFromCode。bat执行EXIT命令,命令窗口将自动关闭。否则,命令窗口将保持打开状态,直到您显式地退出它并退出——java进程一直等到窗口关闭。

#2


4  

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

尝试在start命令前面添加/等待参数。

#3


2  

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

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

Code Example (used in SOAPUI)

代码示例(在SOAPUI中使用)

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

process.waitForProcessOutput()

def exitValue = process.exitValue()

#4


2  

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

#1


35  

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

通过使用start,您是askingcmd。exe在后台启动批处理文件:

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.

因此,在后台进程完成之前,您从Java (cmd.exe)返回的进程将返回。

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

删除start命令,以便在前台运行批处理文件——然后,waitFor()将等待批处理文件完成:

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:

根据OP,有控制台窗口是很重要的——这可以通过添加/等待参数来完成,如@Noofiz所建议的那样。以下SSCCE为我工作:

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.

如果RunFromCode。bat执行EXIT命令,命令窗口将自动关闭。否则,命令窗口将保持打开状态,直到您显式地退出它并退出——java进程一直等到窗口关闭。

#2


4  

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

尝试在start命令前面添加/等待参数。

#3


2  

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

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

Code Example (used in SOAPUI)

代码示例(在SOAPUI中使用)

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

process.waitForProcessOutput()

def exitValue = process.exitValue()

#4


2  

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