【问题标题】:How to open Terminal and execute command using java code in OSX如何在 OSX 中使用 java 代码打开终端并执行命令
【发布时间】:2014-07-02 05:53:12
【问题描述】:

如何在 MAC 中使用 java 代码启动终端并执行一些命令?

我从下面的链接中找到了 LINUX OS 的类似问题。

how to run a command at terminal from java program?

【问题讨论】:

标签: java terminal osx-mavericks


【解决方案1】:

你需要像这样使用 bash 可执行文件来运行它:

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

YouTerminalSoftWareName 必须是绝对路径或在 PATH 环境变量中的区域

【讨论】:

  • Runtime.getRuntime().exec("/bin/bash -c /Applications/Utilities/Terminal.app");我正在这样做但无法打开终端
【解决方案2】:

使用 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-09
    • 2015-07-22
    • 2020-05-14
    • 2018-02-03
    • 2021-12-09
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    相关资源
    最近更新 更多