Java调用外部命令

Java调用外部命令的基本方法是:通过Runtime.getRuntime()返回一个当前环境的运行时,通过运行时来调用外部的命令。

函数原型

调用外部命令可以使用以下命令:

java.lang Class Runtime

java.lang.Object java.lang.Runtime

Process exec(String command) Process exec(String[] cmdarray) Process exec(String[] cmdarray, String[] envp) Process exec(String[] cmdarray, String[] envp, File dir) Process exec(String command, String[] envp) Process exec(String command, String[] envp, File dir)

commandcmdarray是要执行的命令。其中cmdarray[0]是要执行的命令,其它的作为参数。 envp是执行命令附带的环境。 dir是执行命令所在的目录。 命令执行会返回一个Process对象,通过该对象可以与执行命令的子进程进行通信,包括写输入读输出等。

示例代码

下面的代码演示了基本的使用方法,执行一个命令,并读取命令的返回。

public class TestRunTime {  
    public static void main(String[] args) {  
    Process p = Runtime.getRuntime().exec("javac");
    InputStream is = p.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    String line;
    while((line = reader.readLine())!= null){
        System.out.println(line);
    }
    p.waitFor();
    is.close();
    reader.close();
    p.destroy();
    }
}

注意问题

Java执行命令不同于普通的Shell执行命令,一些命令需要自己指定shell执行,带有命令参数的命令最好通过cmdarray的方式执行。 例如:

  1. 命令ping 127.0.0.1 可以写成
Runtime.getRuntime.exec(new String[]{"ping", "127.0.0.1"});
  1. 重定向命令 echo 'hello' > 1.txt 需要写成
Runtime.getRuntime.exec(new String[]{"/bin/sh","-c","echo 'hello' > 1.txt"});

参考内容