[英]Running a command in MacOs terminal from java
I'm trying to run a command line in a mac terminal using java code (particularly a ProcessBuilder). 我正在尝试使用java代码(尤其是ProcessBuilder)在mac终端中运行命令行。
I'm coding in a Windows environment and then exporting it to a Mac into a Jar Executable File. 我正在Windows环境中进行编码,然后将其导出到Mac中的Jar可执行文件中。
I managed to open the terminal using the code posted below, but once there, the commands are not running, the terminal keeps dead, waiting for a keyboard input. 我设法使用下面发布的代码打开终端,但是一旦运行,命令就没有运行,终端就会死机,等待键盘输入。
I will show a simple example for what I want to do: 我将显示一个简单的示例说明我想做什么:
String[] arguments = new String[] {"/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal", "-c", "pwd"};
Process proc = new ProcessBuilder(arguments).start();
The code seems to run fine until it reaches the "-c" parameter, and then it does nothing else. 该代码在到达“ -c”参数之前似乎运行良好,然后什么也不做。
Does someone know how to solve this problem? 有人知道如何解决这个问题吗? Thanks!
谢谢!
EDIT: I've also tried without the "-c", but same result. 编辑:我也试过没有“ -c”,但结果相同。
Try this: 尝试这个:
try {
Runtime.getRuntime().exec("your command");
} catch (IOException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.