0

I'm looking for help with my java code. I would like to start running a .bat from java code someone can help me please, thanks in advance :)

Sawsen
  • 83
  • 2
  • 11

4 Answers4

1
Runtime.getRuntime().exec("cmd /c start build.bat");

refer this : How do I run a batch file from my Java Application?

Community
  • 1
  • 1
backtrack
  • 7,996
  • 5
  • 52
  • 99
1
Runtime.getRuntime().exec("cmd /c start yourbatchfile.bat");
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    when i try this code : i have an error message : windows cannot find "test.bat" make sure you typed the name correctly ... – Sawsen Apr 10 '15 at 09:10
  • This sounds like you are not using the correct _path_ to your file `test.bat`. A quick fix would be for you to put the full name of the file, e.g. `C:\\Directory\\Subdirectory\\yourbatchfile.bat` – Tim Biegeleisen Apr 10 '15 at 09:14
0

Add following code line and it will run the bat batFile.bat

Runtime.getRuntime().exec("cmd /c start batFile.bat");
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60
0

I always do it like this:

Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "build.bat"});

Or use the ProcessBuilder class in Java: http://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html

File batchFile = new File("build.bat"); //Or wherever your file is
ProcessBuilder processBuilder = new ProcessBuilder(batchFile.getAbsolutePath());
Process process = processBuilder.start();

For more info, look at the JavaDoc link :)

Dopdop
  • 37
  • 3