0

I've got a problem that I haven't seen anyone else come across, and that is using exec() to execute a Unix command, but with a variable path rather than a fixed one, when it contains spaces. Most solutions I've seen to paths with spaces in them involve manipulating the string, which I can't do equally with this. Here is my code.

void launchApp(String appName) {//TODO ignore spaces in names!
    String[] cmd1 = new String[] {"chmod +x ", (new File(dataPath(""))).getParentFile().getParentFile().getParentFile().getParentFile().getPath() + "/" + appName + "/" + appName + ".app/Contents/MacOS/" + appName + ""};
    String[] cmd2 = new String[] {"open ", (new File(dataPath(""))).getParentFile().getParentFile().getParentFile().getParentFile().getPath() + "/" + appName + "/" + appName + ".app"};
    saveStrings("log0", cmd1);
    try {
        Runtime.getRuntime().exec(cmd1);
        Runtime.getRuntime().exec(cmd2);
    } catch (IOException e) {
        println(e);
    }
}

(Don't question the really complicated paths... they work just what I need to finish this prototype :) there would be variables to work around anyway)

By the way, I realize I can use ProcessBuilder rather than Runtime.getRuntime().exec() and I've seen that as a solution to the problem, but I have tried it and it doesn't work. I just have it in the exec() format while posting this.

Thank you for any help you can provide!

4
  • Possible related: Spaces in java execute path for OS X. Commented Nov 20, 2017 at 7:05
  • @devpuh different problem, my path contains (many) variables and can’t be solved in that way. Commented Nov 20, 2017 at 7:38
  • Have you read all answers? Please try to simplify cmd1 and cmd2. For example String path = new File(dataPath(""))/*...*/;String cmd1 = {"chmod", "+x", path};/*...*/Runtime.getRuntime().exec(cmd1); Commented Nov 20, 2017 at 7:45
  • @devpuh thank you for your help, after rereading some similar questions I was realizing I was using PathBuilder incorrectly (like PathBuilder("chmod +x", path) rather than separating "chmod" and "+x"), and now it does correctly ignore spaces in the path. Commented Nov 20, 2017 at 15:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.