Java中的空格执行OS X的路径 [英] Spaces in java execute path for OS X

查看:144
本文介绍了Java中的空格执行OS X的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OS X上,我正在尝试.exec的某些东西,但当路径包含一个空格时,它不起作用。我尝试用引号绕过路径,逃避空间,甚至使用\\\ 。



例如,这样做:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆