简体   繁体   English

Java进程无法获取ErrorStream消息

[英]Java Process can't get ErrorStream message

everyone, I have a process that needs to get standard output and log/error/exception output from the subprocess. 每个人,我都有一个需要从子流程中获取标准输出和日志/错误/异常输出的流程。 The standard output is fine, but I can't get ErrorStream, therefore the program is stuck there because of that. 标准输出很好,但是我无法获取ErrorStream,因此程序被卡在那里。 Here is my simple code. 这是我的简单代码。 There is nothing magic, but why can't I get the error stream here? 没有什么魔术,但是为什么我不能在这里得到错误流? Thanks for looking at it. 感谢您的关注。

    BufferedReader standard =
         new BufferedReader(new InputStreamReader(process.getInputStream()));

   BufferedReader error = 
   new BufferedReader(new InputStreamReader(process.getErrorStream()));

    String line = null;
    while ((line = standard.readLine()) != null) {            
        System.out.println(line);
    }
    while ((line = error.readLine()) != null) {           
        System.out.println(line);
    }

Now, as suggested, i used two threads to process the output and error streams, but still had the same problem, as follows. 现在,按照建议,我使用了两个线程来处理输出和错误流,但是仍然存在相同的问题,如下所示。 Can anybody give me some insights? 谁能给我一些见识? Thanks. 谢谢。

    ProcessBuilder pb = new ProcessBuilder(listArgs);
    pb.redirectErrorStream();
    Process process = pb.start();

    StreamThread output = new StreamThread(process.getInputStream());
    StreamThread error = new StreamThread(process.getErrorStream());

    output.start();
    error.start();
    while (true) {
        try {
            output.join();
            break;
        }
        catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

The definition of the StreamThread: StreamThread的定义:

    public static class StreamThread extends Thread{
    private InputStream input = null;   
    public StreamThread(InputStream in){
        input = in;
    }       
    String line = null;
    public void start(){            
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));           
        try{
            while( (line=reader.readLine()) != null ){
                System.out.println(line);
            }
            reader.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

Look at your loops: 看一下你的循环:

while ((line = standard.readLine()) != null) {            
    System.out.println(line);
}
while ((line = error.readLine()) != null) {           
    System.out.println(line);
}

You're going to keep reading from the output stream until it's finished - which is likely to be when the process terminates. 您将继续从输出流中读取数据,直到完成为止-这很可能是进程终止时。 Only then do you start reading the error stream. 只有这样,你开始读错误流。

You should probably put at least one of these into a different thread, so you can read from both streams at the same time. 您可能应该至少将其中之一放入不同的线程中,以便可以同时两个流中读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM