39

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

2
  • Did it work? I have similar issue stackoverflow.com/questions/28917268/… Looking for solution..
    – Nayeem
    Mar 8, 2015 at 13:02
  • Is it work to you? I have the same problem, and even I exec the process like you, when I add '/wait' my program stuck on waiting. Do you have solution?
    – Rat
    Apr 6, 2016 at 15:35

4 Answers 4

53

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.

8
  • 1
    Removing start will hide the CMD, which I don't want to happen. Is there another way to still have the CMD opened while the execution?
    – Maroun
    Mar 4, 2013 at 10:16
  • So you want to see the output from the batch file in the command window? Mar 4, 2013 at 10:19
  • Yes, it is important for this task.
    – Maroun
    Mar 4, 2013 at 10:19
  • 1
    What If I want to launch a third party program, like this; Process p = Runtime.getRuntime().exec("C:\\Program Files\\CCleaner\\CCleaner64.exe"); Calling process.watFor(); Returns immediatly, just like op.
    – Essej
    Jul 19, 2016 at 16:37
  • 1
    @Baxtex This is specific to the application you are launching. For example, I tried the same with Process p = Runtime.getRuntime().exec("c:\\Windows\\System32\\notepad.exe"); and it waits as expected, while I can reproduce that CCleaner64.exe does not wait. I assume that CCleaner64 launches a child process so that the original process which is started with exec() returns immediately. Jul 20, 2016 at 5:34
7

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

0
5

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
  • cmd /c start /wait - This won't execute in Linux - Please suggest us some alternatives for Ubuntu Linux. Jul 18, 2020 at 4:33
4
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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.