Link to home
Start Free TrialLog in
Avatar of rnicholus
rnicholus

asked on

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

This is related to my post here: https://www.experts-exchange.com/questions/24127839/How-to-find-the-latest-file-using-Linux-command-in-Java-program.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

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rnicholus
rnicholus

ASKER

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

Thanks. ! :)
Good :-)