Samy
Samy

Reputation: 323

How make a java program wait until a batch file that is executed with a java process is terminated

i am executing a sequence of batch files, i just need my java program to wait for the first batch file to exit and then execute the next. what happens is every thing got executed without the exit of the file that has been executed before it.

This is my code, i am running it in a loop

String cmd = "cmd /c start /min file1.bat"; 

Process pr = Runtime.getRuntime().exec(cmd);

pr.waitFor();

Upvotes: 2

Views: 4763

Answers (4)

The comment mark as an answer is not correct because it is related with Linux not Windows

import java.util.Date;
public class Test {
    public static void main(final String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println("Executing command " + i + ": " + (new Date()));
            try {
                final Process p = new ProcessBuilder("sh", "-c", "sleep 2;").redirectErrorStream(true).start();
                p.waitFor();
            } catch (final Throwable t) {
                t.printStackTrace();
            }
        }
    }
}

Upvotes: 0

Go Dan
Go Dan

Reputation: 15512

Though I'm using Linux, this should apply to Windows as well. The below sample code executes a shell process multiple times, waiting for each process to complete before continuing:

import java.util.Date;
public class Test {
    public static void main(final String[] args) {
        for (int i = 0; i < 5; i++) {
            System.out.println("Executing command " + i + ": " + (new Date()));
            try {
                final Process p = new ProcessBuilder("sh", "-c", "sleep 2;").redirectErrorStream(true).start();
                p.waitFor();
            } catch (final Throwable t) {
                t.printStackTrace();
            }
        }
    }
}

This outputs:

Executing command 0: Tue Apr 17 09:19:55 EDT 2012
Executing command 1: Tue Apr 17 09:19:57 EDT 2012
Executing command 2: Tue Apr 17 09:20:00 EDT 2012
Executing command 3: Tue Apr 17 09:20:02 EDT 2012
Executing command 4: Tue Apr 17 09:20:04 EDT 2012

Printing the result (exit value) of Runtime.exec(String) using a value of sh -c 'sleep 2;', I get 2 (failed execution); but the result of Runtime.exec(String[]) using new String[] {"sh", "-c", "sleep 2;"} returns 0 (successful execution). Looking at the Runtime.java and ProcessBuilder.java sources, Runtime.exec(String) splits the string out using a StringTokenizer, which uses default delimiters of " \t\n\r\f". So executing Runtime.exec("sh -c 'sleep 2;'") will break the command string out as the array ["sh", "-c", "'sleep", "2;'"], which contains invalid arguments.

To ensure commands are executed properly, it's best to use Runtime.exec(String[]) instead of Runtime.exec(String).

Your cmd /c start /min file1.bat command string is probably not working when split out to ["cmd", "/c", "start", "/min", "file1.bat"]. The following should work:

Runtime.getRuntime().exec(new String[] {"cmd", "/c", "start /min file1.bat"});

Upvotes: 2

Arian
Arian

Reputation: 3241

There is no easy accurate way for this. Java has no control on external programs (and you start such an external prog). You can loop try to get the task out of the taskmanager and if it fails catch the exception and you know when it was terminated.

Or you calculate the time your batfiles will need nd just build in a Thread.sleep();

Upvotes: 0

kaoD
kaoD

Reputation: 1582

start accepts argument /WAIT

Check start /? for more useful arguments (you might want /B instead of /MIN)

Upvotes: 1

Related Questions