0

I'm trying to create an executable .jar that re-opens itself in Mac's Terminal console. (for the sake of having a user interface to enter commands into the program)

// if program is not open in Terminal:
Runtime.getRuntime().exec("java -jar \"" + path + "\" isInConsole");
System.exit(0);

This code executes the command successfully but seamlessly so I don't get the console UI. How can I make it open a visible Terminal window and execute a command in it?

EDIT: I managed to open the Terminal, but still need to figure out how to run the java -jar ... command in it.

This works:

String arg = "cd /Users/potato/Desktop";
Runtime.getRuntime().exec("open -a Terminal --args " + arg);

But this doesn't work:

String arg = "java -jar /Users/potato/Desktop/test.jar isInConsole";
Runtime.getRuntime().exec("open -a Terminal --args " + arg);
potato
  • 995
  • 11
  • 19
  • 1
    Well Terminal is an app and lives under /Applications/Utilities/ to start with – Joakim Danielson Mar 17 '19 at 09:40
  • 1
    If your end goal is to execute a bash command or script then you should probably forget about running Terminal and look at the `ProcessBuilder` class instead to execute your command. – Joakim Danielson Mar 17 '19 at 09:46
  • https://stackoverflow.com/questions/4726001/how-to-run-mac-os-terminal-commands-from-java-using-runtime – howie Mar 17 '19 at 09:46
  • @JoakimDanielson My end goal is to re-open my executable jar inside a Terminal window for the sake of using the console to interact with my program. – potato Mar 17 '19 at 10:02
  • 1
    You probably will need to use AppleScript to launch the Terminal.app and then run your program inside it. See this for example: https://stackoverflow.com/questions/33033606 . You're probably better off *not* trying to do all this from an executable jar. For a simple use case, just stick the jar in a known location somewhere, and write a shell script to launch it and put that shell script on your desktop instead. – Daniel Pryden Mar 17 '19 at 10:47
  • Thank you @DanielPryden using AppleScript solved my problem – potato Mar 17 '19 at 12:02
  • Though I didn't use a shell script file, see my answer to see how I made the .jar executable open itself in Terminal using AppleScript. – potato Mar 17 '19 at 12:16

2 Answers2

0

For creating processes, class Runtime has been superseded by class ProcessBuilder. A very old but still relevant article regarding class Runtime (because it was published before addition of class ProcessBuilder to JDK) is When runtime.exec() won't and is also relevant for class ProcessBuilder.

As stated in the article, method exec() is not a "shell" and so does not parse the command you give it as a single String parameter. You can help the method with parsing by providing an array of Strings.

I suggest you read the article and also the javadoc for class java.lang.ProcessBuilder.

Abra
  • 19,142
  • 7
  • 29
  • 41
0

The code I ended up using executes some AppleScript code: (as DanielPryden suggested)

public static void main(String[] args){
    if(args.length == 0 && System.getProperty("os.name").toLowerCase().contains("mac")){
        try {
            String path = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getAbsolutePath();
            String command = "tell application \"Terminal\"\n" + 
                    "do script \"java -jar \'" + path + "\' isInConsole\"\n" + 
                    "close the front window\n" + // because "do script..." opens another window
                    "activate\n" + 
                    "end tell";
            String[] arguments = new String[]{"osascript", "-e", command};

            Runtime.getRuntime().exec(arguments);
            System.exit(0);
        } catch (IOException | URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // program continues...
}
potato
  • 995
  • 11
  • 19