简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM