Spaces in java execute path for OS X

后端 未结 3 705
庸人自扰
庸人自扰 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:34

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

    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:

    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"});
    

提交回复
热议问题