How to open Terminal and execute command using java code in OSX

。_饼干妹妹 提交于 2019-12-25 02:31:38

问题


How to launch terminal and execute some commands using java code in MAC?

Similar question i have found for LINUX OS from below link.

how to run a command at terminal from java program?


回答1:


It worked same as Linux,

Runtime.getRuntime().exec("rm filename");

Thanks @ginz




回答2:


You need to run it using bash executable like this:

Runtime.getRuntime().exec("/bin/bash -c **YouTerminalSoftWareName**");

YouTerminalSoftWareName must be absolute path or aready in your PATH environment variable




回答3:


Use Apple Script Editor to open Terminal and run your code.

For Example :

String action1 = jsonDriver.readApplicationDetails("OFF");
Runtime runtime = Runtime.getRuntime();
String applescriptCommand = "tell application \"Terminal\"\n" + 
         "activate\n" + "delay 1\n" + "tell application \"System Events\"\n" + 
         "keystroke \"" + action1 + "\"\n" + "end tell\n" + "end tell"; 
String[] args1 = { "osascript", "-e", applescriptCommand };
Process process = runtime.exec(args1);
Thread.sleep(5000);


来源:https://stackoverflow.com/questions/23650193/how-to-open-terminal-and-execute-command-using-java-code-in-osx

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