您需要使用 bash 可执行文件来运行它,如下所示:
Runtime.getRuntime().exec("/bin/bash -c **YouTerminalSoftWareName**");
YouTerminalSoftWareName 必须是绝对路径或已在 PATH 环境变量中
使用 Apple 脚本编辑器打开终端并运行代码。
例如:
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);