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 8893645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:17:51+00:00 2026-06-14T23:17:51+00:00

I have the following code; String[] cmd = { bash, -c, ~/path/to/script.sh }; Process

  • 0

I have the following code;

String[] cmd = { "bash", "-c", "~/path/to/script.sh" };
Process p = Runtime.getRuntime().exec(cmd);

PipeThread a = new PipeThread(p.getInputStream(), System.out);
PipeThread b = new PipeThread(p.getErrorStream(), System.err);

p.waitFor();

a.die();
b.die();

The PipeThread class is quite simple so I will include it in full;

public class PipeThread implements Runnable {

    private BufferedInputStream in;
    private BufferedOutputStream out;

    public Thread thread;

    private boolean die = false;

    public PipeThread(InputStream i, OutputStream o) {
        in = new BufferedInputStream(i);
        out = new BufferedOutputStream(o);
        thread = new Thread(this);
        thread.start();
    }

    public void die() { die = true; }

    public void run() {
        try {
            byte[] b = new byte[1024];
            while(!die) {
                int x = in.read(b, 0, 1024);
                if(x > 0) out.write(b, 0, x);
                else die();
                out.flush();
            }
        }
        catch(Exception e) { e.printStackTrace(); }

        try { 
            in.close();
            out.close();
        }
        catch(Exception e) { }
    }
}

My problem is this; p.waitFor() blocks endlessly, even after the subprocess has terminated. If I do not create the pair of PipeThread instances, then p.waitFor() works perfectly. What is it about the piping of io streams that is causing p.waitFor() to continue blocking?

I’m confused as I thought the IO streams would be passive, unable to keep a process alive, or to make Java think the process is still alive.

  • 1 1 Answer
  • 0 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-06-14T23:17:52+00:00Added an answer on June 14, 2026 at 11:17 pm

    So after much head-scratching, I realised what is happening. p.waitFor() actually isn’t blocking indefinitely, rather my method of checking it is failing; a System.out.println() statement after p.waitFor().

    PipeThread is a class that I had lying around from a previous project, and I have often used it to pipe one stream onto another in a separate thread.

    This must be the first time I have used it with System.out. PipeThread closes whatever streams it is passed upon reading EOF. Both streams in this case included my standard output, so debugging with System.out.println becomes impossible… 🙁

    Weirdly, no IOException was thrown by System.out.println(), I will investigate.

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

Sidebar

Related Questions

I have the following code: std::string cmd = sudo chmod a+x file; int r
I have the following code: public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[]
I have the following C# code: public string TargetDate() { SqlConnection con = new
I have the following code snippet. SqlCommand cmd = new SqlCommand(SELECT FName,LName FROM EMPLOYEE_TABLE
I have the following code: private DataSet GetDataSet(string tableName) { DbCommand cmd = GetConnection().CreateCommand();
I have the following code: String website = http://www.somewebsite.com/; Document doc = Jsoup.connect(website).get(); Elements
I have the following code string three() { return three; } void mutate(string& ref)
I have the following code: string acctStatus = account.AccountStatus.ToString(); if (!SettableStatuses().Any(status => status ==
I have the following code: String serviceType; ServiceBrowser tmpBrowser; for (String playerName: players) {
I have the following code: string[] firstArray = { bla bla, bla bla, bla

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.