Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Execute /cmd /c start path-with-spaces\program.exe

Tags:

java

process

I've read a lot about the question but the answers I have found don't work completely.

I try to run this code :

String[] args = {"cmd","/c","start","C:\\Program Files\\XML Marker\\xmlmarker.exe"};
Runtime rt = Runtime.getRuntime();
ProcessBuilder pb = new ProcessBuilder(args);
Process pr = pb.start();      
//Process pr = rt.exec(args);

As I have spaces in my path, I use String array to pass the arguments to the Process But ... it opens a DOS command window but doesn't launch my program, as if the parameters where ignored

I tried with rt.exec(args) and pb.start() ... same result

Could someone give me some advice please ? Thank you.

like image 514
Hugues Avatar asked Jan 18 '23 20:01

Hugues


1 Answers

Try adding quotes around the path by inserting escaped quotes in your string, as follows:

String[] args = {"cmd","/c","start","\"C:\\Program Files\\XML Marker\\xmlmarker.exe\""};

Notice the \" at the start and end of the path string.

like image 78
Mansoor Siddiqui Avatar answered Jan 30 '23 04:01

Mansoor Siddiqui