Runtime.getRuntime() restart mac Java IM

547 views Asked by At

My os is MAC OS 10.11.3.

I using Spark as our IM(Instant messaging),but when the IM log out,IM give an error,said:

java.io.IOException: Cannot run program "open": error=2, No such file or directory .

But when Eclipse runs it, it worked well,and when I wrapped it a mac package and installed it and run it, click "log out" button, it throws errors.

In program, when I log out,I launch a new IM,meanwhile,I killed the old IM application.

Furthermore,in terminal,no matter what I run how many times “open -a Spark”,it just open the same IM,

The source as follows:

public boolean restartApplicationWithScript() {
    String command = null;
    try {
        if (Spark.isMac()) {
            command = "open -a " + Default.getString(Default.SHORT_NAME);
        }

        String commands[] = {"open", "/Applications/Spark.app"}; 
        Runtime.getRuntime().exec(commands);

        System.exit(0);
        return true;
    } catch (IOException e) {
        Log.error("Error trying to restart application with script", e);
        return false;
    }
}
2

There are 2 answers

5
Stephen C On

The exception seems to be saying that it cannot find the open command. That seems a bit odd, since it is a standard MacOS command. So I suspect that the reason that it cannot be found is that it is not on the PATH.

  • Try giving the absolute pathname for the open command. Running which open should tell you what it is.
  • If open is a shell builtin on MacOS, then you will need to create a subshell to run the command.
0
hongxing ji On

Thanks for all, i solved it by 2 step : 1, cmdline = { "open", "-na", "Microsoft Excel" }; this add a "n" ,otherwise,it will open the same app 。 2,chmod -R 777 youAppPath,if not do this ,the app can't issue the command