We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

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

rnicholus
rnicholus asked
on
Medium Priority
406 Views
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
What does that error "Too many open files" exactly mean actually ?
Why need to wait for a while before getting that exception.
CERTIFIED EXPERT
Top Expert 2016

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

Author

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

Thanks. ! :)
CERTIFIED EXPERT
Top Expert 2016

Commented:
Good :-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.