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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:19:19+00:00 2026-05-24T01:19:19+00:00

I am trying to run some shell scripts for Java by using commons exec

  • 0

I am trying to run some shell scripts for Java by using commons exec package and clear the STDOUT & STDERR buffers by using PumpStreamHandler. Most of the scripts run fine without any problems but some of them hangs.

Particularly those scripts that takes some time to return. My guess is that the PumpStramHandle might be reading end of stream as there is nothing put on the stream for a while and after that the buffers fill up.

Is there any better way to get across this problem?

  • 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-05-24T01:19:20+00:00Added an answer on May 24, 2026 at 1:19 am

    Not the best solution. But does what I need. 🙂

    class OSCommandLogger extends Thread {
        private static final Logger logger = Logger.getLogger(OSCommandLogger.class);
        private volatile boolean done = false;
        private final String name;
        // Each process is associated with an error and output stream
        private final BufferedReader outputReader;
        private final BufferedReader errorReader;
        private final Logger log;
    
        /**
         * Reads the output & error streams of the processes and writes them to
         * specified log
         * 
         * @param p
         * @param name
         * @param log
         */
        OSCommandLogger(Process p, String name, Logger log) {
            // Create readers
            outputReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            this.log = log;
            if (name != null)
                this.name = name;
            else
                this.name = "OSCommandStreamsLogger";
        }
    
        private void logLine(BufferedReader reader, boolean isError) {
            try {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    if (log != null && log.isDebugEnabled()) {
                        if (!isError)
                            log.debug("[OuputStream] " + line);
                        else
                            log.warn("[ErrorStream] " + line);
                    } else
                        logger.debug(line);
                }
            } catch (Exception ex) {
                if (log != null)
                    log.error(name + ":" + "Error while reading command process stream", ex);
            }
        }
    
        public void run() {
            while (!done) {
                logLine(outputReader, false);
                logLine(errorReader, true);
    
                try {
                    // Sleep for a while before reading the next lines
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    log.debug("Done with command");
                }
            }
    
            // Process is done. Close all the streams
            try {
                logLine(outputReader, false);
                outputReader.close();
    
                logLine(errorReader, true);
                errorReader.close();
                if (log != null && log.isDebugEnabled())
                    log.debug(name + ": Closed output/ error Streams.");
    
            } catch (IOException ie) {
                if (log != null)
                    log.error(name + ":" + "Error while reading command process stream", ie);
            }
        }
    
        public void stopLoggers() {
            if (log != null && log.isDebugEnabled())
                log.debug(name + ":Stop loggers");
            this.done = true;
        }
    }
    

    Usage:

    Process p = Runtime.getRuntime().exec("Command");
    OSCommandLogger logger = new OSCommandLogger(p, "Command", log);
    
    // Start the thread using thread pool
    threadExec.executeRunnable(logger);
    int exitValue = p.waitFor(); // Wait till the process is finished
    
    // Required to stop the logger threads
    logger.stopLoggers();
    logger.interrupt();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a shell script that, when run, will set some environment
I'm trying to run some commands in paralel, in background, using bash. Here's what
I'm trying to run some third party bash scripts from within my ruby program.
I'm trying to run some queries to get rid of XSS in our database
I am trying to run some unit tests in a C# Windows Forms application
I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually
Where would set_include_path(../../); point to? I am trying to run some PHP code on
I've run into some problems when trying to serialize my object to XML. The
I am trying to run PartCover to test the coverage of some tests with
I'm trying to build my first generic list and have run into some problems.

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.