Solved

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

Posted on 2010-11-29
12
1,305 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:FerrousHombre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34233285
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
 

Author Comment

by:FerrousHombre
ID: 34233370
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
 
LVL 92

Expert Comment

by:objects
ID: 34234076
>             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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:FerrousHombre
ID: 34234430
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
 
LVL 92

Expert Comment

by:objects
ID: 34234484
running from the command line is not the same as running from exec()
whats the command?
0
 

Author Comment

by:FerrousHombre
ID: 34234632
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34234659
What is the command when you run it from the command line
0
 
LVL 92

Expert Comment

by:objects
ID: 34234756
Be aware that exec()/ProcessBuilder do not use a shell and you need to manage the environment yourself
0
 

Author Comment

by:FerrousHombre
ID: 34235328
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 34235339
>>BufferedReader.readLine() causes the program to stop responding, though.

That will block until more data are available to read
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 34235354
>  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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34285734
:)
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 140
import as existing maven project 3 41
Coding for the first time 9 70
Find out all repos that a user is most active on Github 1 33
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question