java.io.Exception: Too many open file when running 'ls -lrt' in Java program

Posted on 2009-02-12
Medium Priority
Last Modified: 2012-05-06
This is related to my post here: http://www.experts-exchange.com/Programming/Languages/Java/Q_24127839.html
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
  • 3
  • 2
LVL 86

Accepted Solution

CEHJ earned 1000 total points
ID: 23624666
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

ID: 23625057
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

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

Author Comment

ID: 23643718
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

ID: 23644054
Good :-)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

621 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