我估计是标准输出缓冲区已经满了的缘故,这错误输出缓冲区和标准输出缓冲区如果填满的情况下是会阻塞JAVA线程的。
在你的类中加入如下代码:static class StreamGobbler extends Thread
{
InputStream is;
String type;
StringBuffer info;
StreamGobbler(InputStream is, String type,StringBuffer info)
{
this.is = is;
this.type = type;
this.info=info;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
info.append(type + ">" + line);
} catch (Exception ioe)
{
ioe.printStackTrace();
}
}
}
public void rumCommand(String cmd) {
Runtime runtime = Runtime.getRuntime();
String cr=System.getProperty("line.separator");
StringBuffer output=new StringBuffer();
StringBuffer err=new StringBuffer();
int exitValue=0;
try {
Process proc = runtime.exec(cmd);
// put a BufferedReader on the ls output
StreamGobbler sgStd=new StreamGobbler(proc.getInputStream(),"STDOUT",output);
StreamGobbler sgErr=new StreamGobbler(proc.getErrorStream(),"ERROUT",err);
sgStd.start();
sgErr.start();
exitValue=proc.waitFor();
} catch (IOException e) {
logger.debug(e);
} catch (InterruptedException e) {
logger.debug(e);
}
}
你自己的代码只要使用:
runCommand(dumpCommand);
参考:http://hellsing42.iteye.com/blog/118317
2008年7月22日 15:44
821
0
0
4