Saahon Saahon - 1 year ago 135
Java Question

Run batch with java return error

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) {

this is the restart.bat:

cd /d %~dp0
java -jar device.jar --atm.username=%2 --atm.password=%3 -debug

restart.bat need at first killed process where PID = processID
and go to folder where is located he
and running device.jar with parameters.
When I runnin this code on Windows 7 machine, code is worked. If running on Windows XP SP3 machine return error "Not Found C:\Documents ...."
I need running this code on Windows XP machine. where the error is, why the code does not work?

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download