Java中的空格执行OS X的路径 [英] Spaces in java execute path for OS X
问题描述
例如,这样做:
Runtime.getRuntime()。exec(open /foldername/toast.sh);
但是如果有空格,那么这些都不行:
Runtime.getRuntime()。exec(open / folder name / toast.sh);
Runtime.getRuntime()。exec(open \/文件夹名/ toast.sh\);
Runtime.getRuntime()。exec(open / folder\\ name / toast.sh);
Runtime.getRuntime()。exec(open /folder\\\ name/toast.sh);
想法?
编辑:转义反斜杠
在 Sun的论坛 ...似乎是一个非常常见的问题,不限于OS X.
线程中的最后一个帖子总结了提出的解决方案。实质上,使用 Runtime.exec
的形式,它需要一个 String []
array:
String [] args = new String [] {open,\/文件夹名/ toast.sh\};
或(论坛建议这也会工作)
String [] args = new String [] {open,folder name / toast.sh};
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.
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.
There's a summary of this problem on Sun's forums... seems to be a pretty common issue not restricted to OS X.
The last post in the thread summarizes the proposed solution. In essence, use the form of Runtime.exec
that takes a String[]
array:
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" };
这篇关于Java中的空格执行OS X的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!