Run batch file from Java code
Rather than Runtime.exec(String command)
, you need to use the exec(String command, String[] envp, File dir)
method signature:
Process p = Runtime.getRuntime().exec("cmd /c upsert.bat", null, new File("C:\\Program Files\\salesforce.com\\Data Loader\\cliq_process\\upsert"));
But personally, I'd use ProcessBuilder
instead, which is a little more verbose but much easier to use and debug than Runtime.exec()
.
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "upsert.bat");
File dir = new File("C:/Program Files/salesforce.com/Data Loader/cliq_process/upsert");
pb.directory(dir);
Process p = pb.start();
try following
try {
String[] command = {"cmd.exe", "/C", "Start", "D:\\test.bat"};
Process p = Runtime.getRuntime().exec(command);
} catch (IOException ex) {
}