Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProcessBuilder and command with space

I recently updated my Java version to JDK7u21. In the release notes of update 21, it is clearly mentioned about issue using Runtime.exec.

I therefore want to change my code to use ProcessBuilder. I am trying to execute a command with spaces in it. But even if using ProcessBuilder for this, I land up in C:\Users\Parag.Joshi\Documents and not the exact directory.

Below is my code:

ProcessBuilder p = new ProcessBuilder("cmd", "/c", "explorer ", "C:\Local Disk D\My Tutorial");
p.start();

I had a look at Java execute a command with a space in the pathname but didn't get a clue.

like image 368
ParagJ Avatar asked Dec 03 '25 18:12

ParagJ


1 Answers

I just tested it on my local machine.

the behaviour is caused because of the space after "explorer ". remove that space and it will work. Also you need to quote the \.

ProcessBuilder p = new ProcessBuilder("cmd", "/c", "explorer", 
                                      "C:\\Local Disk D\\My Tutorial");
like image 198
A4L Avatar answered Dec 06 '25 06:12

A4L