Java 中的空格执行 OS X 的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/697621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:25:33  来源:igfitidea点击:

Spaces in java execute path for OS X

javaruntimeescapingspace

提问by Glenn

On OS X, I am trying to .exec something, but when a path contains a space, it doesn't work. I've tried surrounding the path with quotes, escaping the space, and even using \u0020.

在 OS X 上,我试图 .exec 一些东西,但是当路径包含空格时,它不起作用。我试过用引号包围路径,转义空格,甚至使用 \u0020。

For example, this works:

例如,这有效:

Runtime.getRuntime().exec("open /foldername/toast.sh");

But if there's a space, none of these work:

但如果有空间,这些都不起作用:

Runtime.getRuntime().exec("open /folder name/toast.sh");

Runtime.getRuntime().exec("open \"/folder name/toast.sh\"");

Runtime.getRuntime().exec("open /folder\ name/toast.sh");

Runtime.getRuntime().exec("open /folder\u0020name/toast.sh");

Ideas?

想法?

Edit: Escaped backslash... still no worky.

编辑:转义反斜杠......仍然没有工作。

回答by Jarret Hardie

There's a summary of this problem on Sun's forums... seems to be a pretty common issue not restricted to OS X.

Sun 的论坛上有这个问题的摘要......似乎是一个非常普遍的问题,不仅限于 OS X。

The last post in the thread summarizes the proposed solution. In essence, use the form of Runtime.execthat takes a String[]array:

该线程中的最后一篇文章总结了建议的解决方案。本质上,使用Runtime.execString[]数组的形式:

String[] args = new String[] { "open", "\"/folder name/toast.sh\"" }; 

or (the forum suggests this will work too)

或者(论坛建议这也可以)

String[] args = new String[] { "open", "folder name/toast.sh" };

回答by Paul Tomblin

Try this:

试试这个:

Runtime.getRuntime().exec("open /folder\ name/toast.sh");

"\ " will just put a space in the string, but "\ " will put a "\ " in the string, which will be passed to the shell, and the shell will escape the space.

"\" 只会在字符串中放置一个空格,而 "\" 会在字符串中放置一个 "\",该字符串将传递给 shell,而 shell 会转义该空格。

If that doesn't work, pass in the arguments as an array, one element for each argument. That way the shell doesn't get involved and you don't need bizarre escapes.

如果这不起作用,请将参数作为数组传递,每个参数一个元素。这样 shell 就不会参与进来,你也不需要奇怪的转义。

Runtime.getRuntime().exec(new String[]{"open", "/folder name/toast.sh"});

回答by Paul Gregtheitroade

Paul's option works, but you still must escape the spaces like so:

Paul 的选项有效,但您仍然必须像这样转义空格:

Runtime.getRuntime().exec(new String[]{"open", "/folder\ name/toast.sh"});

The thing that sucks about using a String array is that each param and its option must be in their own element. For instance you cannot do this:

使用 String 数组的糟糕之处在于每个参数及其选项必须在它们自己的元素中。例如你不能这样做:

Runtime.getRuntime().exec(new String[]{"executable", "-r -x 1", "/folder\ name/somefile"});

But instead must specify it like so:

但必须像这样指定它:

Runtime.getRuntime().exec(new String[]{"executable", "-r", "-x", "1", "/folder\ name/somefile"});