nank oro
nank oro

Reputation: 11

Run batch File using Java (space in arguements issue)

I am trying to run a batch file from Java. The following works:

Process p1 = java.lang.Runtime.getRuntime().exec("D:\\Users\\xx\\Documents\\NetBeansProjects\\Test1\\New folder\\batch.bat"); 

The following does not work:

Process p1 = java.lang.Runtime.getRuntime().exec("D:\\Users\\xx\\Desktop\\Jar Test\\New folder\\batch.bat");

The error I get is "Windows cannot find 'D:\Users\xx\Desktop\Jar'. Make sure you typed the name correctly, and then try again."

Although there are spaces in both paths, for some reason the second one is not running. I have read numerous threads about escaping spaces, but none of the solutions there worked with me.

Note: I have tried to use process builder but I am also facing the same issue.

Upvotes: 1

Views: 347

Answers (1)

Danny Daglas
Danny Daglas

Reputation: 1491

Try

Process p1 = Runtime.getRuntime().exec(new String[] {"D:\\Users\\xx\\Documents\\NetBeansProjects\\Test1\\New folder\\batch.bat"});

This form of exec() wont tokenize your input by spaces for you.

Upvotes: 1

Related Questions