0

I have gone through few questions raised on how to achieve this. I used process.waitFor() and /wait as mentioned here. The problem is by doing so it waits not just till the command is executed but until cmd prompt is closed (can be done by adding exit in the bat file). But I cannot modify bat file as its a Product file.

Runtime run = Runtime.getRuntime();

    try {
        String path = "C:/Folder/c.bat";
        String executeCmd= "cmd /c start /wait "+path;
        final Process process =run.exec(executeCmd);        
        process.waitFor();
         System.out.println("did I wait?");
    } catch (Exception e) {         
        e.printStackTrace();
    }

How to make it wait only till the command is executed.

Community
  • 1
  • 1
2FaceMan
  • 443
  • 2
  • 18
  • 34
  • Take out `start /wait`? (It looks like you're already waiting for cmd.exe to close.) – Bill_Stewart Jul 02 '14 at 19:04
  • @Bill_Stewart if I remove `start` , it doesn't get executed , if I remove `wait` . it doesn't even wait and prints _did I wait?_ – 2FaceMan Jul 02 '14 at 19:08
  • Then it depends on what `C:/Folder/c.bat` is doing. If it is launching an executable asynchronously, then you won't be able to wait for that executable to terminate without finding out its process ID and then waiting for it to close. – Bill_Stewart Jul 02 '14 at 19:16
  • @Bill_Stewart Your point is valid,Suppose if I go with the above logic that I mentioned.Is there anyway I can check if the prompt is back and if so I can proceed.In my case bat file is stopping and starting application server – 2FaceMan Jul 02 '14 at 19:26
  • To do what you suggest would be a very awkward and brittle solution. A much better solution is to find the executable's process ID and wait for it to close. – Bill_Stewart Jul 02 '14 at 20:19
  • How can I find the PID and wait for it ? – 2FaceMan Jul 03 '14 at 08:53
  • 1
    I'm not a Java programmer, but presumably there are some classes available. I would ask a new question with your specific goal in mind. For example: "I am launching a shell script (batch file) from a Java program, and the shell script I am running launches an executable. How can I wait for that executable to terminate before continuing in my program?" – Bill_Stewart Jul 03 '14 at 14:03

4 Answers4

0

You can create a helper batch file with following content:

start /wait %1\c.bat
exit

Store this helper batch anywhere you want to.

Then start this helper batch file with the path to c.bat as its parameter.

   Runtime run = Runtime.getRuntime();

    try {
      String pathToCBatch = "C:\\Folder\\"; 
      String pathToHelperBatch = "c:/helperBatch.bat";
      String executeCmd = "cmd /c start /wait " + pathToHelperBatch + " " + pathToCBatch;
      final Process process = run.exec(executeCmd);
      System.out.println(System.currentTimeMillis());
      process.waitFor();
      System.out.println(System.currentTimeMillis());
    } catch (Exception e) {
      e.printStackTrace();
    }
s3b
  • 158
  • 1
  • 10
  • Its not working as expected,waits for the command prompt to close . – 2FaceMan Jul 03 '14 at 08:52
  • What exactly are you trying to achieve then? Should the program wait or not? – s3b Jul 03 '14 at 11:07
  • the .bat file is used to start server, I want the code to wait till it starts. The logic you have written will wait until the cmd promt is closed manually , I tried it – 2FaceMan Jul 03 '14 at 17:09
0

I have also the same issue: "call a Batch file and wait until it's finished" (Windows PC). This solution works for me :

StringBuilder command = new StringBuilder("cmd /c start /wait C:\\script.bat");
    // my script take 2 file as arguments
    command.append(" ").append(inputFile);
    command.append(" ").append(outputFile);
    try {
        final Process p = Runtime.getRuntime().exec(command.toString());
        p.waitFor();
    } catch (InterruptedException e) {
        System.out.println(e);
    }
Searly
  • 9
  • 5
0

dont work for me to exit cmd from java i have come with a solution i added "exit" in my .bat file at the end and it works now

example : this is my "adservice.google.com.bat" bat file which dont exit after executing from java

netsh advfirewall firewall add rule name="adservice.google.com.bat"  protocol=any dir=out action=block remoteip=2404:6800:4009:80f::2002,216.58.203.34

so i have to add "exit" in it , in the last line

netsh advfirewall firewall add rule name="adservice.google.com.bat"  protocol=any dir=out action=block remoteip=2404:6800:4009:80f::2002,216.58.203.34
exit
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 07 '22 at 21:49
0

also if the directory has space in it then the command wont work from string of arrey

so instead do this

File file = new File("E:\\NetBeans Projects\\Test.bat");

String[] command = {"cmd.exe", "/c", "start",  file.getName() };
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(command , null , file.getParentFile());