下边这段代码在跑到while循环的时候就死在那儿不动了,是为什么?
BufferedReader这个带缓冲区的Reader在这样用的时候有什么问题吗?我是想得到命令输出的所有内容,所以加了个while循环,但是每次都在while那里就死了,不动了。这是为什么呢?
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test
{
public static void main(String[] args)
{
Runtime rt = Runtime.getRuntime();

try
{
Process p = rt.exec("cmd.exe /k cleartool co"); BufferedReader stdOut = new BufferedReader(new InputStreamReader(p
.getInputStream()), 1);
BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()), 1);

String result = null;
String line = null;
while ( (stdOut.ready() && (line = stdOut.readLine()) != null)
|| (stdError.ready() && (line = stdError.readLine()) != null)) 
    {
result += line + "\n";
    }
stdOut.close();
stdError.close();
p.waitFor();
System.out.println(result);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

解决方案 »

  1.   

    你先直接用cmd运行下你的指令看报错没
      

  2.   

    直接运行是没有错的。但是在这里边运行就卡住了,偶尔会卡,概率还比较大。我分析了一下,可能是rt在运行时,p是一直处于运行状态,p不退出,stdError和stdOut都是存在的,而p的退出是在while循环之后,所以存在在while处一直等待的现象,但这个问题该这么解决呢?
      

  3.   

    你可以试一下另起线程处理的方式:
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;public class Test { private static Thread threadin; private static Thread threaderr;
    private static Process process; public static void main(String[] args) {
    String cmd = "cmd.exe  /k cleartool co";
    try {
    process = Runtime.getRuntime().exec(cmd);
    } catch (IOException e1) {
    throw new RuntimeException("call System Process Error :"
    + e1.getMessage(), e1);
    }
    threadin = new ReadThread(process.getInputStream(), "[INPUT]", false);
    threaderr = new ReadThread(process.getErrorStream(), "[ERROR]", true);
    threadin.start();
    threaderr.start();
    try {
    process.waitFor();
    } catch (InterruptedException e) {
    stopProcess();
    } finally {
    process = null;
    }
    try {
    threadin.join();
    threaderr.join();
    } catch (InterruptedException e) {
    //
    }
    } public static void stopProcess() {
    Process r = process;
    if (r != null) {
    r.destroy();
    } if (threadin != null) {
    threadin.interrupt();
    } if (threaderr != null) {
    threaderr.interrupt();
    } } static class ReadThread extends Thread { private final InputStream inputStream; private final String cName; private final boolean isErrorOut; ReadThread(InputStream iStream, String cName, boolean isErrorOut) { this.inputStream = iStream;
    this.cName = cName;
    this.isErrorOut = isErrorOut;
    } public void run() {
    BufferedInputStream stream = new BufferedInputStream(
    this.inputStream);
    Reader reader = new InputStreamReader(stream);
    BufferedReader reader2 = new BufferedReader(reader); String readmsg = null;
    try {
    while ((readmsg = reader2.readLine()) != null
    && !isInterrupted()) {
    System.out.println("read message="+readmsg+" It is :"+isErrorOut);
    }
    } catch (IOException e) {
    throw new RuntimeException(cName + "=" + e.getMessage(), e);
    }
    }
    }
    }