Java中执行本地命令的接口方法有两种,一种是java.lang.Runtime获取Runtime环境执行exec方法,另一种是通过创建java.lang.ProcessBuilder实例,执行command方法,这两种方法都能创建Process对象获取Input、Output、Error流与本地进程通信。
java.lang.ProcessBuilder是Jdk 1.5中新添加的一个类,J2SE 1.5之前,都是由java.lang.Runtime来实现进程的控制管理。
无论通过哪种方法创建,都是获取了Process对象,但是ProcessBuilder可以通过redirectErrorStream(true)把错误流合并到InputStream流中。一般命令执行成功后,会从inputstream中输出,但在Windows操作系统上执行本地java命令,成功后从ErrorStream流输出,因此需要通过redirectErrorStream将ErrorStream合并到Inputstream中去。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class TestRuntime {
public static void main(String[] args) throws IOException {
Process process=Runtime.getRuntime().exec(args);
exeProcess(process);
System.out.println("===============ProcessBuilder redirectErrorStream(false)======================");
Process process2= new ProcessBuilder().redirectErrorStream(false).command(Arrays.asList(args)).start();
exeProcess(process2);
System.out.println("===============ProcessBuilder redirectErrorStream(true)======================");
Process process3= new ProcessBuilder().redirectErrorStream(true).command(Arrays.asList(args)).start();
exeProcess(process3);
}
public static void exeProcess(Process process) throws IOException
{
BufferedReader isr=new BufferedReader(new InputStreamReader(process.getInputStream()));
String line="";
while((line=isr.readLine())!=null)
{
System.out.println(line);
}
BufferedReader isr1=new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line1="";
while((line1=isr1.readLine())!=null)
{
System.out.println("error:"+line1);
}
process.destroy();
}
}
以上代码在eclipse中调试运行后为:
error:java version "1.8.0_45"
error:Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
error:Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)
===============ProcessBuilder redirectErrorStream(false)======================
error:java version "1.8.0_45"
error:Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
error:Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)
===============ProcessBuilder redirectErrorStream(true)======================
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)
在Cmd模式下运行runnable jar为
Microsoft Windows [版本 6.2.9200]
(c) 2012 Microsoft Corporation。保留所有权利。
C:\Users\lili>cd c:\test
c:\test>java -jar newProcessTest.jar java -version
error:java version "1.8.0_45"
error:Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
error:Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode)
===============ProcessBuilder redirectErrorStream(false)======================
error:java version "1.8.0_45"
error:Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
error:Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)
===============ProcessBuilder redirectErrorStream(true)======================
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)
c:\test>