关键字: java
Process 类型对象的 waitFor() 方法的时候当前线程会被阻塞掉,因为InputStream in = Process.getInputStream();或 InputStream err = Process.getErrorStream(); 这2个流中的一个会被阻塞住,所以解决的办法可以是用多线程来解决,可以参看http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html。
但还有另一种方法可以尝试,
Java代码
- String[]cmds={...};//commandandarg
- ProcessBuilderbuilder=newProcessBuilder(cmds);
- //bothreadinputstreamanderrstream
- builder.redirectErrorStream(true);
- Processprocess=builder.start();
- BufferedReaderbr=newBufferedReader(newInputStreamReader(process
- .getInputStream()));
- StringreadLine=br.readLine();
- while(readLine!=null){
- System.out.println(readLine);
- readLine=br.readLine();
- }
- intresultValue=process.waitFor();
builder.redirectErrorStream(true); 和BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); 这样使用就可以在一个线程中执行,而保证不被阻塞住。