我需要在 Fedora 16 的终端上从 JAVA 程序运行命令。我尝试使用
Runtime.getRuntime().exec("xterm");
但这只是打开终端,我无法执行任何命令。
我也试过这个:
OutputStream out = null;
Process proc = new ProcessBuilder("xterm").start();
out = proc.getOutputStream();
out.write("any command".getBytes());
out.flush();
但我仍然只能打开终端,但无法运行命令。 关于如何做的任何想法?
最佳答案
你需要像这样使用 bash
可执行文件来运行它:
Runtime.getRuntime().exec("/bin/bash -c your_command");
更新: 正如 xav 所建议的那样,建议使用ProcessBuilder而是:
String[] args = new String[] {"/bin/bash", "-c", "your_command", "with", "args"};
Process proc = new ProcessBuilder(args).start();
关于java - 如何从java程序在终端运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15356405/