但一写就发现,事情并没有那么的简单。我又找到了一篇比较老的文章,仔细阅读,才明白了其中的细节。下面是一些要点:
等待命令执行结束用waitFor(),其返回值就是命令的返回值。
如果出现程序执行被挂起,没有任何反应的情况,是由于没有读取命令子进程的正常输出流或错误输出流导致缓冲区被占满,进程被锁住。这个时候需要把输出流中的内容给读出来。最好的做法是使用两个线程,分别同时读取正常输出流和错误输出流。
执行Windows平台上的命令时使用cmd.exe /C,如cmd.exe /C dir。
记得关闭命令子进程的输出流,通过Process.getOutputStream().close(),这样不会导致命令子进程被锁住。
1. Runtime.exec() 有四种调用方法
* public Process exec(String command);
* public Process exec(String [] cmdArray);
* public Process exec(String command, String [] envp);
* public Process exec(String [] cmdArray, String [] envp);
2. 得到程序执行返回值, 0为success
需要用waitFor()函数,比如
Process p = Runtime.getRuntime().exec("javac");
(处理.....)
int exitVal = p.waitFor();
3. 得到程序执行的结果或错误信息
需要用BufferedInputStream 和 BufferReader来得到,否则程序会hang
比如得到错误信息用p.getErrorStream(),然后输出即可:
BufferedInputStream in = new BufferedInputStream(p.getErrorStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
这里还有一点,就是得到process的输出的方式是getInputStream,这是因为我们要从Java 程序的角度来看,外部程序的输出对于Java来说就是输入,反之亦然。
4. Runtime.exec() 不等同于直接执行command line命令!
啊,我算是在这里吃了苦头了。Runtime.exec()很有局限性,对有些命令不能直接把command line里的内容当作String参数传给exec().
比如重定向等命令。举个例子:
javap -l xxx > output.txt
这时要用到exec的第二种重载,即input 参数为String[]:
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","javap -l xxx > output.txt"});
5.demo
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; class StreamDrainer implements Runnable { private InputStream ins; public StreamDrainer(InputStream ins) { this.ins = ins; } public void run() { try { BufferedReader reader = new BufferedReader( new InputStreamReader(ins)); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } } public class TestRunCmd { public static void main(String[] args) { String[] cmd = new String[] { "cmd.exe", "/C", "wmic process get name" }; try { Process process = Runtime.getRuntime().exec(cmd); new Thread(new StreamDrainer(process.getInputStream())).start(); new Thread(new StreamDrainer(process.getErrorStream())).start(); process.getOutputStream().close(); int exitValue = process.waitFor(); System.out.println("返回值:" + exitValue); } catch (Exception e) { e.printStackTrace(); } } }
6.demo2
public class StreamGobbler extends Thread { InputStream is; String type; StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) System.out.println(type + ">" + line); } catch (IOException ioe) { ioe.printStackTrace(); } } }
public class GoodWindowsExec { public static void main(String args[]) { if (args.length < 1) { System.out.println("USAGE: java GoodWindowsExec"); System.exit(1); } try { String osName = System.getProperty("os.name"); String[] cmd = new String[3]; if (osName.equals("Windows NT")) { cmd[0] = "cmd.exe"; cmd[1] = "/C"; cmd[2] = args[0]; } else if (osName.equals("Windows 95")) { cmd[0] = "command.com"; cmd[1] = "/C"; cmd[2] = args[0]; } Runtime rt = Runtime.getRuntime(); System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]); Process proc = rt.exec(cmd); // any error message? StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR"); // any output? StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT"); // kick them off errorGobbler.start(); outputGobbler.start(); // any error??? int exitVal = proc.waitFor(); System.out.println("ExitValue: " + exitVal); } catch (Throwable t) { t.printStackTrace(); } } }
参考链接:
http://jiangshuiy.iteye.com/blog/1674235
http://blog.csdn.net/flying881114/article/details/6272472
http://walsh.iteye.com/blog/449051