• 0

run bat file from java program


Question

2 answers to this question

Recommended Posts

  • 0

Ok, here's a quick example that runs an external command and outputs the exit code.

One thing to bear in mind here is that the 'waitFor' method means the parent app will halt until the child app has completed (useful if you need the output to make decisions from prior to resuming).

This was just a quick google, more detail can be found here (including a non-halting example): http://www.rgagnon.com/javadetails/java-0014.html


import java.io.*;
public class CmdExec {
public static void main(String argv[]) {
try {
String line;
Process p = Runtime.getRuntime().exec("test.cmd");
p.waitFor();
System.out.println(p.exitValue());
}
catch (Exception err) {
err.printStackTrace();
}
}
}
[/CODE]

This topic is now closed to further replies.