1

I am currently trying to execute a .bat file in Java. The bat file actually runs perfectly but after it is executed, my main program exits. I have no idea why and i need it to carry on with its code once the bat file has been executed. The program and bat file will all be run on Windows. Here is the code to how i run the bat file:

if(!running) {
   @SuppressWarnings("unused")
   Process process = Runtime.getRuntime().exec("cmd /c start "+localTargetInPath+"\\startBatchClient.bat");

   process.getInputStream().close();
   process.getOutputStream().close();
   process.getErrorStream().close();
}
//wait to allow batch client to start up
Thread.sleep(1000);

I have been trying to figure this out for days with no luck. Any ideas?

EDIT:

Just to clarify something stated in answers:

The point is to do the following:

  1. Run batch file
  2. batch file launches batch client
  3. Java program continues to run
  4. Java program connects to running batch client

EDIT 2:

Here is the contents of the batch file (which again runs the application without any problems:

set JAVA_HOME="C:\Program Files\Java\jdk1.6.0_23"
set ep_root=C:\app\plm\e6_1_1\P2\BatchClient
set axalant_root=C:\app\plm\e6_1_1\P2\BatchClient\axalant
set batchcli_root=C:\app\plm\e6_1_1\P2\BatchClient
set EP_MACH=intel-ms-nt4.0

C:\Windows\system32\cmd.exe /K c:\app\plm\e6_1_1\P2\BatchClient\axalant\cmd\batchclient_nowrapper.cmd  -eciservermode -p %localInPath%\batchClient.properties -T C:\zftmp\svc.emea.batch11e\testclient.properties
Barranka
  • 20,547
  • 13
  • 65
  • 83
DaRoGa
  • 2,196
  • 8
  • 34
  • 62
  • possible duplicate of [How do I run a batch file from my Java Application?](http://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application) – ASA May 06 '14 at 13:24
  • 2
    remove `start`. You are spawning 2 `cmd` process and waiting for the first while the second runs free. – ixe013 May 06 '14 at 13:25
  • Have you tried putting a try catch block around the code? – Priyesh May 06 '14 at 13:25
  • @Traubenfuchs I wouldnt say its a duplicate because on that question the batch file wont even run. As stated in my question I just need to stop my java program exiting once the batch file has been run. @Priyesh I have a big try/catch block around my code which should catch any error. However no error is thrown, the program just terminates as if i called `System.exit(0)` but I dont. And @ixe013 I have just tried it taking out the `start` command but the result is the same. – DaRoGa May 06 '14 at 13:30
  • What flow control (loop, etc) do you have that keeps your primary thread going? – Big Al May 06 '14 at 17:50
  • It is a program that should constantly run, therefore the main code segment is inside a while(true) loop – DaRoGa May 07 '14 at 06:51
  • 1
    @DaRoGa Please don't vandalize posts (including your own). If you want, you can delete your own post. – Barranka Mar 13 '15 at 17:26
  • I cannot delete it as it has answers. I need them deleting due to sensitive information and i have used all my flags on otger questions for moderators to delete them – DaRoGa Mar 13 '15 at 17:28

3 Answers3

0

If you don't need to do anything concurrently you can just use the waitFor function in Process. The problem you are probably having is that your current thread does not wait for the batch thread to finish.

0

The Runtime.exec() method spawns a process, but doesn't wait for it to complete.

You should use the waitFor() method on the returned Process instance, in order to wait for the process to end.

Plus, @ixe013's comment is correct: you should remove the start word from the command, as it spawns yet another process. If you don't remove start, then waitFor() won't help you much: it will wait for the start command to end, which is instantaneous.

Isaac
  • 16,458
  • 5
  • 57
  • 81
  • Sorry just to clarify, the batch file launches an application, which I then need to interact with. So as I understand it, `waitFor()` would never return as the launched application needs to remain open. Is that correct? – DaRoGa May 06 '14 at 13:33
  • @DaRoGa I think that you should rephrase your question to describe *exactly* what you're trying to achieve. Seems like I'm not the only one having difficulties understanding... – Isaac May 06 '14 at 13:35
  • Yes apologies I have added an edit to the question to describe what the program should be doing. I hope this clarifies it a little – DaRoGa May 06 '14 at 13:37
  • @DaRoGa Put debug prints after the call to `exec()`. If the batch file is indeed invoked, but a debug print right after `exec()` fails to print, then perhaps an exception is being thrown and you're not handling it properly. Can't think of any other reason. You should consider posting some more code. – Isaac May 06 '14 at 13:40
  • I am catching Exception as well as specific exception so it should always be caught and give me some output but it doesnt. And the program exits just before `Thread.sleep(1000)`. I did have some debug output that confirmed this. I would post more code but I do not think it would be relevant as all the code before it is executed perfectly and it stops in the middle of the provided code – DaRoGa May 06 '14 at 13:46
  • I have edited my question to include the contents of the batch file – DaRoGa May 06 '14 at 13:50
0

How about:

File batchFile = new File("path\script.bat");
ProcessBuilder processBuilder = new ProcessBuilder(batchFile.getAbsolutePath());
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
int exitStatus = process.waitFor();
System.out.println("Processed finished with status: " + exitStatus);
KnightWhoSayNi
  • 570
  • 1
  • 5
  • 14
  • Again, I believe Im right in saying that waitFor will not work in my case due to EDIT in my question. batch file loads a program -> java code then needs to communicate with that program without closing it. Therefore waitFor() will never return – DaRoGa May 06 '14 at 14:01