2

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();
2
  • Maybe removing "start" would do the trick, see stackoverflow.com/questions/2448402/… Commented Apr 17, 2012 at 13:24
  • @VilmantasBaranauskas thanks.. it works with this String cmd = "cmd /c file1.bat"; Commented Apr 17, 2012 at 14:44

4 Answers 4

2

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"});
Sign up to request clarification or add additional context in comments.

2 Comments

hi Dan Cruz still there is problem with "start /min" my tread is running with out ending for the last batch file.
@Sam Sorry, I'm not much of a Windows person. You might want to try different start options. I do recall having issues in the past with BAT files and using exit in the BAT file to properly use them with the start command. But that was years ago and I haven't used Windows since then.
1

start accepts argument /WAIT

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.