3

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.

Community
  • 1
  • 1
ParagJ
  • 1,566
  • 10
  • 38
  • 56

1 Answers1

5

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");
A4L
  • 17,353
  • 6
  • 49
  • 70
  • Perfect! Yes, I forgot to escape \. I had escaped it in my code although. But it is a space problem after explorer. Thanks. – ParagJ May 07 '13 at 10:22
  • You're welcome! Sure you did escape `\\` otherwise the code wouldnt't have executed and shown you the home dir, i added it just for the sake of completeness ;-) – A4L May 07 '13 at 10:25