I need send parameter and execute batch file from java code. I used this method:
private void run(){
if (atmUsernameField.getText().length() > 0 &&
atmPasswordField.getText().length() > 0 &&
serverURLField.getText().length() > 0){
String atmUsername= atmUsernameField.getText();
String atmPassword = atmPasswordField.getText();
String url = serverURLField.getText();
String userHomePath = System.getProperty("user.home");
userHomePath = userHomePath + File.separator + "INFOKIOSK" + File.separator + "device_jar";
String fileName = userHomePath + File.separator + "restart.bat";
if (SystemUtils.IS_OS_WINDOWS_XP || SystemUtils.IS_OS_WINDOWS) {
try {
String processID = ManagementFactory.getRuntimeMXBean().getName();
int endIndex = processID.indexOf("@");
processID = processID.substring(0, endIndex);
new ProcessBuilder("cmd", "/c", "start " + fileName, processID, atmUsername, atmPassword, url).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TASKKILL /F /PID %1 /T
cd /d %~dp0
java -jar device.jar --atm.autoload.page=%4 --atm.username=%2 --atm.password=%3 -debug
userHomePath
contains spaces on winXP.
On Win7 it does not anymore.
Try to add quotes as shown below, it will work in either environment:
new ProcessBuilder("cmd", "/c", "start \"" + fileName + "\"", processID, atmUsername, atmPassword, url).start();
Maybe you'll need to add quotes in your .bat too
cd /d "%~dp0"