如何在 OSX 中打开终端并使用 java 代码执行命令

问题描述 投票:0回答:2

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

我从下面的链接找到了有关 LINUX 操作系统的类似问题。

如何从java程序在终端运行命令?

java terminal osx-mavericks
2个回答
0
投票

您需要使用 bash 可执行文件来运行它,如下所示:

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

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


0
投票

使用 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);
© www.soinside.com 2019 - 2024. All rights reserved.