How can I keep a ProcessBuilder Process from locking up when processing output streams?

I've found numerous fixes posted for this issue online all stating that the processing must be done inside of a thread.  I setup the code to do this, but it still locks up.  BufferedReader.readLine() seems to be the root of the issue, whenever I'm stepping through the code in debug mode, things freeze up there.  The code is included below:

    public void method {
            ....
            proc = processBuilder.start();
            StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(),"OUTPUT");
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(),"ERROR");
            outputGobbler.start();
            errorGobbler.start();
            proc.destroy();
            ....
    }

    class StreamGobbler extends Thread {
        InputStream is;
        String type;

        StreamGobbler(InputStream is, String type){
            this.is = is;
            this.type = type;
        }

        @Override
        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line = null;
                while((line = br.readLine()) != null){
                    System.out.println(type + ">" + line);
                }
                br.close();
            } catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
    }

Open in new window

FerrousHombreAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
>>BufferedReader.readLine() causes the program to stop responding, though.

That will block until more data are available to read
0
 
sweetfa2Commented:
Are you sure it is not the proc.destroy() causing your problem.

Should you not be waiting for completion of your gobbler threads before destroying proc?
0
 
FerrousHombreAuthor Commented:
I've tried calling proc.waitFor() before the proc.destroy() call, but it doesn't seem to make any difference, the code just chugs and chugs.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
objectsCommented:
>             proc.destroy();

you shouldn't be calling destroy()

> whenever I'm stepping through the code in debug mode, things freeze up there.

it will block there while it waits for output from the process, that is expected. It will block until the process completes.

The problem would seem more likely to be with the process you are running. It sounds like it does not complete.
0
 
FerrousHombreAuthor Commented:
No, when I run the commands for that ProcessBuilder in the command line, they run very quickly.  Also, commenting out the proc.destroy() call didn't change anything.
0
 
objectsCommented:
running from the command line is not the same as running from exec()
whats the command?
0
 
FerrousHombreAuthor Commented:
It's just a .ksh script that returns the status of another process.

I realize that the command line is not the same as running from exec().  I mentioned that the command runs quickly there because you said it sounded like the command doesn't complete.  Also, I'm not running from exec(), I'm starting a Process via ProcessBuilder.start().
0
 
CEHJCommented:
What is the command when you run it from the command line
0
 
objectsCommented:
Be aware that exec()/ProcessBuilder do not use a shell and you need to manage the environment yourself
0
 
FerrousHombreAuthor Commented:
I did go through and make sure that the BufferedReader gets closed, the Process gets destroyed, etc.  I still have a circumstance where BufferedReader.readLine() causes the program to stop responding, though.
0
 
objectsConnect With a Mentor Commented:
>  I still have a circumstance where BufferedReader.readLine() causes the program to stop responding, though.

As I mentioned earlier that will stop responding until the process completes. That is expected.

It is simply waiting for output from the process
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.