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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:36:58+00:00 2026-05-15T20:36:58+00:00

The offending block of code is below. The code almost always works, but sometimes

  • 0

The offending block of code is below. The code almost always works, but sometimes it hangs forever. The application is an EJB timer bean.

Actually, it has only hung once and I can’t reproduce it. It’s worked without any problems in production for almost 2 years. But, while testing an updated version of the app, the timer just froze after running for a few days and never released the database locks from it’s last run. The logs clearly show that it froze somewhere in the block of code below. The command it’s running is ‘chmod’.

public void shellExec(String cmd, File workDir) {
    String s = null;
    try {
        Process p = Runtime.getRuntime().exec(cmd, null, workDir);
        int i = p.waitFor();
        if (i == 0){
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            // read the output from the command
            while ((s = stdInput.readLine()) != null) {
                logger.debug(s);
            }
        }
        else {
            BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            // read the output from the command
            while ((s = stdErr.readLine()) != null) {
                logger.debug(s);
            }
        }
    } catch (Exception e) {
        logger.debug(e);
    }
}

I’m hesitant to modify this code because it’s been tested and has worked correctly for almost two years. I also can’t reproduce the problem so I’d have no idea whether a rewritten version is any better. But, it’s pretty clear that it could hang and I don’t know what the likelihood is.

From googling the issue, it seems like that block of code is pretty standard for executing a shell command. Is there any kind of known problem with that code? Does anyone know of a good way to make sure that it will throw an exception rather than hang, considering that I can’t reproduce the problem?

Thanks.

  • 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-15T20:37:00+00:00Added an answer on May 15, 2026 at 8:37 pm

    You need to execute the consumption of stdout/err concurrently. Otherwise you’ll get the blocking behaviour that you see. See this answer for more info.

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

Sidebar

Related Questions

I'm getting a really strange NPE exception. This is the offending code: private static
I've got a program that worked until recently. The offending code is shown here:
Larry Osterman writes about a real error found in Microsoft's code, the offending code
I'm trying to use a for_each loop in my code, but I'm getting the
The offending code: template<typename T> class SharedObject { public: typedef boost::intrusive_ptr<T> Pointer; typedef boost::intrusive_ptr<T
When you create a new MFC application, the wizard creates the following block of
The offending URLs are: (Doesn't work) http://alltheragefaces.com/face/surprised-wut/ (Works) http://alltheragefaces.com/face/surprised-wut The .htaccess rule I have
The offending line str.replace(/ /g, ) gives Error: In orders.js.erb.coffee, Parse error on line
I am writing some code in Python something like this: import sys try: for
Does anybody know how to find offending web part which causes this error ?

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.