Solved Too many open file when running 'ls -lrt' in Java program

Posted on 2009-02-12
Last Modified: 2012-05-06
This is related to my post here:
Where I want to get the latest file using Linux command in my Java program.

The problem is after running it for about one day, it starts throwing "IOException: Too many open files".
How should I handle this?
I have closed resources after I used it.
Process process = Runtime.getRuntim().exec(new String[] { "bash", "-c", "ls -lrt /directory1/subdirectory | tail -1" });


BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));



// Close resources.

try { if ( stdInput != null ) { stdInput.close(); stdInput = null; } } catch (Exception ex) {}

try { if ( stdError != null ) { stdError.close(); stdError = null; } } catch (Exception ex) {}

Open in new window

Question by:rnicholus
    LVL 86

    Accepted Solution

    I would suggest using finally blocks, not only for each Reader but for the Process, on which you should call destroy.

    If you look at the output of the following, you might find unterminated processes:
    ps faux | less

    Open in new window


    Author Comment

    What does that error "Too many open files" exactly mean actually ?
    Why need to wait for a while before getting that exception.
    LVL 86

    Expert Comment

    The OS will allow you a certain number of file handles, after which it'll start complaining

    Author Comment

    I've been running the program for a while now after adding call to destroy() method. It seems to solve the problem.

    Thanks. ! :)
    LVL 86

    Expert Comment

    Good :-)

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 67
    triangle challenge 4 38
    count7 challenge 12 52
    Unable to start eclipse ? 17 49
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now