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

pho*_*nix 26 java linux terminal

我需要从JAVA程序在Fedora 16的终端上运行命令.我试过用

Runtime.getRuntime().exec("xterm"); 
Run Code Online (Sandbox Code Playgroud)

但这只是打开终端,我无法执行任何命令.

我也试过这个:

OutputStream out = null;
Process proc = new ProcessBuilder("xterm").start();
out = proc.getOutputStream();  
out.write("any command".getBytes());  
out.flush(); 
Run Code Online (Sandbox Code Playgroud)

但我仍然只能打开终端,但无法运行命令.关于如何做的任何想法?

Sud*_*hul 36

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

Runtime.getRuntime().exec("/bin/bash -c your_command");
Run Code Online (Sandbox Code Playgroud)

更新: 正如xav所建议的那样,建议使用ProcessBuilder:

String[] args = new String[] {"/bin/bash", "-c", "your_command", "with", "args"};
Process proc = new ProcessBuilder(args).start();
Run Code Online (Sandbox Code Playgroud)

  • 现在不建议使用`Runtime.exec()`:use应该使用ProcessBuilder(http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang .String [],%20java.lang.String [],%20java.io.File%29) (3认同)

shy*_*an1 13

我投票支持Karthik T的答案.您不需要打开终端来运行命令.

例如,

// file: RunShellCommandFromJava.java
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class RunShellCommandFromJava {

    public static void main(String[] args) {

        String command = "ping -c 3 www.google.com";

        Process proc = Runtime.getRuntime().exec(command);

        // Read the output

        BufferedReader reader =  
              new BufferedReader(new InputStreamReader(proc.getInputStream()));

        String line = "";
        while((line = reader.readLine()) != null) {
            System.out.print(line + "\n");
        }

        proc.waitFor();   

    }
} 
Run Code Online (Sandbox Code Playgroud)

输出:

$ javac RunShellCommandFromJava.java
$ java RunShellCommandFromJava
PING http://google.com (123.125.81.12): 56 data bytes
64 bytes from 123.125.81.12: icmp_seq=0 ttl=59 time=108.771 ms
64 bytes from 123.125.81.12: icmp_seq=1 ttl=59 time=119.601 ms
64 bytes from 123.125.81.12: icmp_seq=2 ttl=59 time=11.004 ms

--- http://google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 11.004/79.792/119.601/48.841 ms
Run Code Online (Sandbox Code Playgroud)


Chr*_*per 10

您实际上不需要从xterm会话运行命令,您可以直接运行它:

String[] arguments = new String[] {"/path/to/executable", "arg0", "arg1", "etc"};
Process proc = new ProcessBuilder(arguments).start();
Run Code Online (Sandbox Code Playgroud)

如果进程以交互方式响应输入流,并且您想要注入值,那么执行之前执行的操作:

OutputStream out = proc.getOutputStream();  
out.write("command\n");  
out.flush();
Run Code Online (Sandbox Code Playgroud)

不要忘记最后的'\n',因为大多数应用程序将使用它来识别单个命令输入的结束.


use*_*434 5

正如其他人所说,你可以在没有xterm的情况下运行外部程序.但是,如果要在终端窗口中运行它,例如让用户与之交互,xterm允许您指定要作为参数运行的程序.

xterm -e any command
Run Code Online (Sandbox Code Playgroud)

在Java代码中,这变为:

String[] command = { "xterm", "-e", "my", "command", "with", "parameters" };
Runtime.getRuntime().exec(command);
Run Code Online (Sandbox Code Playgroud)

或者,使用ProcessBuilder:

String[] command = { "xterm", "-e", "my", "command", "with", "parameters" };
Process proc = new ProcessBuilder(command).start();
Run Code Online (Sandbox Code Playgroud)