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

  • Home
  • SEARCH
  • 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 7714165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:03:40+00:00 2026-06-01T02:03:40+00:00

I am writing a process that will run continuously, getting messages on a queue

  • 0

I am writing a process that will run continuously, getting messages on a queue which cause it to call an executable and process its output. I am using Runtime.getRuntime().exec() to invoke it, but I am only getting output the first time. To more easily show the issue, I have put it in a loop. I am using ‘seq 1 5’ as a standin for the executable, which will print the numbers 1 to 5 each on a separate line. (I have run it on both Mac 10.7 and Fedora 14.)

    public static void main(String args[]) {
    for (int i = 0; i < 4; i++) {
        try {
            Process process = Runtime.getRuntime().exec("seq 1 5");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while (reader.ready() || errReader.ready()) {
                if (reader.ready()) {
                    System.out.println("Process output: " + reader.readLine());
                }
                if (errReader.ready()) {
                    System.err.println("Process error: " + errReader.readLine());
                }
            }
            reader.close();
            errReader.close();
            int result = process.waitFor();
            if (result != 0) {
                System.err.println("Process returned with result " + 0);
            }
            System.out.println("Finished process for iteration " + i);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Here is the result – you can see the expected output of the process only occurs in the first loop:

Process output: 1
Process output: 2
Process output: 3
Process output: 4
Process output: 5
Finished process for iteration 0
Finished process for iteration 1
Finished process for iteration 2
Finished process for iteration 3

Occasionally, perhaps one out of 10 runs, one of the later iterations will print output as well – but never all of them. Obviously there must be something that doesn’t get cleaned up correctly, hence causing all but the first exec() to misbehave, but I don’t know what it could be. The input streams both get closed.

  • 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-01T02:03:41+00:00Added an answer on June 1, 2026 at 2:03 am
    import java.io.*;
    public class TP {
        public static void main(String... argv) {
            ProcessBuilder pb = new ProcessBuilder(argv);
            for (int i = 0; i < 4; i++) {
                try {
                    Process process = pb.start();
                    int result = process.waitFor();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                    while (reader.ready() || errReader.ready()) {
                        if (reader.ready()) {
                            System.out.println("Process output: " + reader.readLine());
                        }
                        if (errReader.ready()) {
                            System.err.println("Process error: " + errReader.readLine());
                        }
                    }
                    reader.close();
                    errReader.close();
                    if (result != 0) {
                        System.err.println("Process returned with result " + result);
                    }
                    System.out.println("Finished process for iteration " + i);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a PHP process that will run on a Unix machine that will
I'm in the process of writing a cross platform c# application that will run
I am writing a Web app that will need to run a background process
For writing scripts for process automation in Linux platform, which scripting language will be
I'm writing a wrapper that forks an execv() process. Output from the child is
I'm writing some pretty CPU-intensive, concurrent numerical code that will process large amounts of
I am writing an application in Qt using python that will process a txt
For the first time, I am writing a web service that will call upon
I'm writing a windows service that once started will run every X hours. The
I am writing an assembly function to be called from C that will call

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.