public class Test { public static void main(String[] args) throws IOException, InterruptedException { String command = “cmd /k D:\ProgramFiles\workspace_eclipse\Java_Maintenanec_System\src\com\dengdie\util\script\rebootwinserver\UpdatePackages.bat”; Process proc = Runtime.getRuntime().exec(command); proc.getOutputStream().close(); StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), “Error”); StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), “Output”); errorGobbler.start(); outputGobbler.start(); proc.waitFor(); } } 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) { if (type.equals(“Error”)) System.out.println(line); else System.out.println(line); } } catch (IOException ioe) { ioe.printStackTrace(); } } }
原文地址:http://my.oschina.net/u/946001/blog/292718