• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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

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

0
rnicholus
Asked:
rnicholus
  • 3
  • 2
1 Solution
 
CEHJCommented:
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

0
 
rnicholusAuthor Commented:
What does that error "Too many open files" exactly mean actually ?
Why need to wait for a while before getting that exception.
0
 
CEHJCommented:
The OS will allow you a certain number of file handles, after which it'll start complaining
0
 
rnicholusAuthor Commented:
I've been running the program for a while now after adding call to destroy() method. It seems to solve the problem.

Thanks. ! :)
0
 
CEHJCommented:
Good :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now