Solved

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

Posted on 2010-11-29
12
1,326 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

691 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