2

im trying to run a bat file and it dosent do anything. When i open the bat file manually by double clicking on it in Windows, it opens a cmd windod that runs a CLI. The bat file needs to run jar.

Here is my code:

@Override
public void changeToCli() {

        try {

            Process p = Runtime.getRuntime().exec("C:\\Users\\Gleb\\workspace1\\MVP\\CLI\\RunMe.bat");

        } catch (Exception e) {
            e.printStackTrace();
        }

}
Gal Sosin
  • 714
  • 7
  • 27

2 Answers2

0

You need to handle it's input, output and error streams correctly.

The batch file may be waiting for some input, or there might be output on the batch's output or error stream that's larger than the buffer, causing the batch to block until the output / error output is consumed.

Note that this means consuming the input (it's program's output, named input on the Java side) and error streams in two separate threads. See Printing Runtime exec() OutputStream to console for hint on how to avoid the need to handle that yourself.

Community
  • 1
  • 1
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • By far the easiest way to do this is with [ProcessBuilder.inheritIO()](http://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html#inheritIO--). – VGR Oct 08 '15 at 16:42
0

(a) use ProcessBuilder instead of Runtime, e.g. like here.

(b) bat file is a script, actually cmd.exe executes these files

cmd.exe /c path/to/your.bat
Community
  • 1
  • 1
ursa
  • 4,404
  • 1
  • 24
  • 38