如何从 Java 运行 UNIX 终端并向其发送命令?
关于该主题,下面的代码
Process proc = null;
try {
String[] cmdss= {"gnome-terminal"};
proc = Runtime.getRuntime().exec(cmdss, null, wd);
} catch (IOException e) {
e.printStackTrace();
}
从 Ubuntu 运行终端。
运行终端后如何向终端发出命令?
例如:运行终端并运行“ls”等命令。
Regarding the topic, the code below
Process proc = null;
try {
String[] cmdss= {"gnome-terminal"};
proc = Runtime.getRuntime().exec(cmdss, null, wd);
} catch (IOException e) {
e.printStackTrace();
}
Runs the terminal form Ubuntu.
How do I issue commands into the terminal after running the termnal?
eg: running the terminal and run command such as "ls" etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以在命令行上给 gnome-terminal 一些选项,让它执行什么。
-x
选项为您提供了大致相同的好处,但您可以将命令行拆分为单独的单词。-e
和-x
都使用可选参数运行程序,同时将程序的标准输入和输出连接到终端。这样用户就可以与终端正常交互。示例:
这将打开终端并运行“程序”
bash
。bash
将获得两个参数:-c
和ls;回声……;阅读
。-c
选项使 bash 解析并执行下一个参数。这将调用ls
,然后调用echo ...
,然后调用等待返回键的read
。在 Java 中,您必须将参数适当地分割成一个数组,如下所示:
You can give
gnome-terminal
some options on the command line what it shall execute.The
-x
option gives you roughly the same benefit but you can split the commandline into separate words.Both
-e
and-x
run the program with optional arguments while connecting the program`s standard input and output to the terminal. So the user can interact with the terminal properly.Example:
This will open the terminal and run the "program"
bash
.bash
will get two arguments:-c
andls; echo ....; read
. The-c
option makes bash parsing and executing the next argument. This will callls
, thenecho ...
thenread
which waits for the return key.In Java you must split the arguments appropriately into an array like this: