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"
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72

4 Answers4

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();
Community
  • 1
  • 1
Mikel
  • 24,855
  • 8
  • 65
  • 66
  • 6
    ProcessBuilder is the recommended way, as it can deal with spaces much better than Runtime.exec() –  Feb 06 '11 at 23:45
  • I actually just added an example using ProcessBuilder based on the Javadocs. Does it look right to you? – Mikel Feb 06 '11 at 23:47
  • 1
    I tried using this, but It still doesn't work. Do I have to specify the full path to the executable? – LanguagesNamedAfterCofee Feb 07 '11 at 00:07
  • 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. – Mikel Feb 07 '11 at 00:09
  • I would additionally note that this answer (hopefully) avoids command injection attacks. If your process uses /bin/sh, you should lookout for command injection. https://www.owasp.org/index.php/Command_injection_in_Java – notalbert Feb 07 '17 at 02:31
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.

Bert F
  • 85,407
  • 12
  • 106
  • 123
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"});
wattostudios
  • 8,666
  • 13
  • 43
  • 57
Hoverfrog
  • 133
  • 1
  • 1
  • 8
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"});

Madhusudan N
  • 147
  • 1
  • 4