Search code examples
javamacosruntimeprocessbuilder

open -n with ProcessBuilder


I am trying to open an application in MacOS with Java with the following command using ProcessBuilder:

ProcessBuilder process = new ProcessBuilder("open -n /Applications/Utilities/Terminal.app");
        try {
            process.start();
        } catch (IOException ex) {
            logger.debug("IOException: {}", ex);
            SdkException.notify(ex.getClass().getName(), ex, getClass(), Severity.ERROR);
        }

It doesnt start the application, but It starts when I run with Runtime.getRuntime().exec

try{
    Process p = Runtime.getRuntime().exec("open -n /Applications/Utilities/Terminal.app");
} catch (IOException ex){
    logger.debug("IOException: {}", ex);
}

How to make this works with ProcessBuilder?


Solution

  • Split the command arguments into separate strings. Instead of

    ProcessBuilder process = new ProcessBuilder("open -n /Applications/Utilities/Terminal.app");

    use

    ProcessBuilder process = new ProcessBuilder("open", "-n", "/Applications/Utilities/Terminal.app");