0

I have a batch file located at C:\app\stuff\dostuff.bat that does stuff to the file system based on the arguments you provide it with. When I open a command line, and run it like so, it works perfectly fine:

C:\app\stuff\dostuff.bat zombies

Like I said, this invocation works perfectly (and properly handles the zombies arg). However, I am now trying to drive the execution of dostuff.bat from inside a Java app like so:

try {
    Process process = Runtime.getRuntime().exec("C:\\app\\stuff\\dostuff.bat zombies");
    process.waitFor();
} catch (IOException|InterruptedException e) {
    logger.error(ExceptionUtils.getFullStackTrace(e));
}

When I run this, I get no exceptions, but it is clearly not properly executing the batch script (I can tell this because the rest of the file system that the batch script touches doesn't get modified like it should). Am I invoking this incorrectly? Do I need to do something with forking processes? How might I debug?

smeeb
  • 27,777
  • 57
  • 250
  • 447

1 Answers1

1

Try it as follows

Process process = Runtime.getRuntime().exec("cmd /c start C:\\app\\stuff\\dostuff.bat zombies");
René Winkler
  • 6,508
  • 7
  • 42
  • 69
  • Thanks @Rene Winkler (+1) however this causes an undesirable command prompt to open and then hang there after the batch file finishes executing. Any ideas as to how to fix (it can't stay like that, as is!). – smeeb Mar 16 '15 at 18:48
  • insert at the very end of your bat-file `exit` – René Winkler Mar 16 '15 at 18:53