Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 567817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:07:25+00:00 2026-05-13T13:07:25+00:00

In pseudocode, here’s what I’m doing: Process proc = runtime.exec(command); processOutputStreamInThread(proc.getInputStream()); processOutputStreamInThread(proc.getErrorStream()); proc.waitFor() However,

  • 0

In pseudocode, here’s what I’m doing:

Process proc = runtime.exec(command);
processOutputStreamInThread(proc.getInputStream());
processOutputStreamInThread(proc.getErrorStream());
proc.waitFor()

However, sometimes processOutputStreamInThread doesn’t see any output and sometimes it does. Roughly, the method creates a BufferedInputStream of the command’s output and sends it to a logger.

Based on what I’m seeing, I’m guessing that command need not have all it’s output dumped into the streams fed by getInputStream() and getErrorStream(), thus allowing the stream to be empty.

The results of my trials are the following questions:

(1) Does waitFor() in java.lang.Process require the executed program’s output to have been read before it returns?

The documentation only states:

causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

(2) Under what conditions do the streams provided by getInputStream and getErrorStream need to be closed and/or are they closed automatically?

The documentation only states:

Gets the error stream of the subprocess. The stream obtains data piped from the error output stream of the process represented by this Process object.

Implementation note: It is a good idea for the input stream to be buffered.

One user reports that he had to close the streams himself, but I get an exception at least part of the time indicating that the stream is already closed when I attempt to do so.

Edit: changed getOutputStream to getInputStream, now present above.

Resolution: The problem ended up being that in certain cases the threads used to process the output stream wouldn’t run until after my very short-lived process had completed, resulting in the input stream giving me no data. waitFor didn’t do any waiting for the output of the executed program. Rather, the program ran and terminated before any output could be gathered.

I used threads because I’m not sure how much output I was going to get on standard error and standard output and I wanted to be able to process both simultaneously, without blocking one or the other should only one of them have data available. But, because my threads can’t consistently read the executed program’s output, it’s a non-solution.

My final coded looked something like this:

ProcessBuilder pb = new ProcessBuilder(cmdargs);
pb.redirectErrorStream(true);
Process proc = pb.start();
processOutputStream(proc.getInputStream());
proc.waitFor()
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:07:26+00:00Added an answer on May 13, 2026 at 1:07 pm

    If your external process expects something on its stdin, you MUST close the getOutputStream. Otherwise you will waitFor forever.

    Here is the When Runtime.exec() won’t article from JavaWorld which describes different pitfalls of exec method and how to avoid them.

    From my experience it’s better to consume STDOUT and STDERR of child process ( until they EOF ) and then block in waitFor. Hopefully at this point you won’t have to waitFor long.

    An answer to Kaleb’s question.
    Under normal conditions you shouldn’t close the streams, however because you are waitingFor and for whatever reason it does not have a timeout, you may need to close these streams if you encounter some error conditions in the output and don’t want to process child’s output further. However, whether the child program will terminate (crash) when it’s STDOUT or STDERR pipe is closed at the other end is totally up to that child’s implementation. Most shell programs though, will terminate under such conditions.

    I really wish waitFor had some meaningful timeout though and the Process had a documented way of cleaning up its resources when you’ve decided to abandon its monitoring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the basic nature of what i'm doing. Note: this is pseudocode.. this
So here's what I'm trying to do in pseudocode: array = new Array(); thisObj
I am wondering if anyone here can help me with some pseudocode or at
Here are two ways of calling callscript (in pseudocode): using duplicate calls if flag
Here's what I would like to do in pseudocode form. Bind Two Functions to
Here is pseudocode for what I am hoping to do: Open text File Find
What am i doing wrong here? Java code for computing prefix function. Two input
here is a pseudoCode 1. View myView = new View(); 2. Animation myAnimation =
Here's some pseudocode for what I want my makefile to do: if (A doesn't
Here is the Pseudocode for Lempel-Ziv-Welch Compression. pattern = get input character while (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.