它使用一个类读取执行的程序生成的所有输出,并将其显示在其自己的stdout中。
class StreamGobbler extends Thread { InputStream is; // reads everything from is until empty. StreamGobbler(InputStream is) { this.is = is; } 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(line); } catch (IOException ioe) { ioe.printStackTrace(); } }}Runtime rt = Runtime.getRuntime();Process proc = rt.exec("javac");//output both stdout and stderr data from proc to stdout of this processStreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream());StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream());errorGobbler.start();outputGobbler.start();proc.waitFor();
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)