Open a Terminal window with Java

限于喜欢 提交于 2019-12-10 11:44:01

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/23956389/open-a-terminal-window-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!