Do I need to close stream and how when use Runtime.getRuntime().execdirectly?

Y

yuanyun.ken

occasionally, after a long period, our product throws "Too many open
files".
So I want to make sure the code closes all stream after having used
it.

And from http://www.vnoel.com/Computers-Related-Issues/Java-problem-Too-many-open-files.html,
http://www.velocityreviews.com/forums/t139735-too-many-open-named-pipes-in-a-java-process.html

I see Runtime.getProcess() method automatically opens three streams
(stdout, stderr, stdin) each time the getProcess() is called. It is
the responsibility of the caller to close those streams when done.

But if I just call it directly like this:
Runtime.getRuntime().exec("chmod 777 " + getFileName());
and doesn't get the returned Process and doesn't call
process.getInputStream()
Do I still need to close stream and how?

Any help is appreciated.
 
A

Arne Vajhøj

yuanyun.ken said:
occasionally, after a long period, our product throws "Too many open
files".
So I want to make sure the code closes all stream after having used
it.

And from http://www.vnoel.com/Computers-Related-Issues/Java-problem-Too-many-open-files.html,
http://www.velocityreviews.com/forums/t139735-too-many-open-named-pipes-in-a-java-process.html

I see Runtime.getProcess() method automatically opens three streams
(stdout, stderr, stdin) each time the getProcess() is called. It is
the responsibility of the caller to close those streams when done.

But if I just call it directly like this:
Runtime.getRuntime().exec("chmod 777 " + getFileName());
and doesn't get the returned Process and doesn't call
process.getInputStream()
Do I still need to close stream and how?

Good question.

I would expect "no".

But I doubt that it is specified anywhere.

If you want to know for a specific implementation then check
the source.

If I look at the ProcessImpl class for SUN Java 1.6 then it
looks as if one should get the streams and explicit close
them.

Arne
 
E

EJP

yuanyun.ken said:
But if I just call it directly like this:
Runtime.getRuntime().exec("chmod 777 " + getFileName());
and doesn't get the returned Process and doesn't call
process.getInputStream()
Do I still need to close stream and how?

Yes. You need to close all three streams. You also need to wait for the
child process, otherwise it becomes a zombie.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top