17

How can I execute a Java System (shell) command that has a space in the pathname?

I've tried putting quotes and a backslash (), But it does not work.

ln -s "dir1/dir2" "my\ dir/dir2"
4
  • "my\ dir/dir2" is not a valid string in Java because "\ " is not a valid escape sequence. Try "my\\ dir/dir2". Commented Feb 6, 2011 at 23:26
  • No, that's the output when I println the string. I'm already using "\\ " Commented Feb 6, 2011 at 23:27
  • Ah, okay. That was not clear. Are you using OS X by any chance? stackoverflow.com/questions/697621 Commented Feb 6, 2011 at 23:28
  • I'm confused - is there literal a backslash and a space in the second path or just a space (and the backslash is intended to "escape" the space)? Commented Feb 6, 2011 at 23:45

4 Answers 4

26

By far the most reliable way is to use Runtime.exec(String[] cmdarray).

If you use Runtime.exec(String command), Java only splits the command on whitespace.

the command string is broken into tokens using a StringTokenizer created by the call new StringTokenizer(command) with no further modification of the character categories. The tokens produced by the tokenizer are then placed in the new string array cmdarray, in the same order.

See also g++: File not found

Or use ProcessBuilder something like this:

ProcessBuilder pb = new ProcessBuilder("ln", "-s", "dir1/dir2", "my dir/dir2");
Process p = pb.start();
Sign up to request clarification or add additional context in comments.

5 Comments

ProcessBuilder is the recommended way, as it can deal with spaces much better than Runtime.exec()
I actually just added an example using ProcessBuilder based on the Javadocs. Does it look right to you?
I tried using this, but It still doesn't work. Do I have to specify the full path to the executable?
How about you try it and tell us? :-) By looking at the value returned by p.waitFor() or p.exitValue(), it should be obvious whether there was an error, and if so what the error was. For example, if the exit value is 127, then yes, it couldn't find the command and you should try changing it to /bin/ls. getErrorStream will have more information as well.
I would additionally note that this answer (hopefully) avoids command injection attacks. If your process uses /bin/sh, you should lookout for command injection. owasp.org/index.php/Command_injection_in_Java
7

Do you really need to execute it in a shell (e.g. do you need to shell expansion of things like ~ or *, etc)? If not, you could invoke ln directly:

Process p =
    Runtime.getRuntime()
    .exec(new String[]{"/bin/ln","-s","dir1/dir2", "my\\ dir/dir2"});

If you really need a shell, try this (this may need a little tweaking depending on how the shell processes the quotes):

Process p =
    Runtime.getRuntime()
    .exec(new String[]{"/bin/sh", "-c", "ln -s \"dir1/dir2\" \"my\\ dir/dir2\""});

Edit:

I was under the impression the second path has a literal backslash in it. If it's not supposed to remove the \\ from the string literals above.

Comments

1

None of these work on Lion. However, the following does work, and is backwards compatible for Tiger.

Runtime.getRuntime().exec(new String[]{"/bin/bash","-c","/path/to/file/space*init"});

Comments

0

You can use it in the following way without having to introduce any backslashes: Runtime.getRuntime().exec(new String[]{"ln", "-s", "dir1/dir2", "my dir/dir2"});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.