Search code examples
javamacosterminalwindow

Open a Terminal window with Java


I am aware that you can pass commands to the Terminal and it returns your result in Java. The problem is for undisclosed reasons I need a Terminal window to open and to have the command in it. This code works to open an empty Terminal window (on mac):

p = Runtime.getRuntime().exec("/usr/bin/open -a Terminal");

And I have seen someone say on a different overflow question that you can do this:

p = Runtime.getRuntime().exec("/usr/bin/open -a Terminal [Put Command to pass through here]");

But it didn't seem to work when I tried to pass the command through to the window.

Any help or suggestions?


Solution

  • You can achieve this by using AppleScript.

    Here you can find example how call it from Java: AppleScript from Java on Mac OS X 10.6?

    And script you need will look like this:

    tell application "Terminal"
        set currentTab to do script ("cd ~/Downloads/")
        set currentTab to do script ("ll")
    end tell