Spaces in java execute path for OS X

后端 未结 3 683
庸人自扰
庸人自扰 2021-01-02 09:05

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 usin

3条回答
  •  有刺的猬
    2021-01-02 09:52

    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.

    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.

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

提交回复
热议问题