Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

14 views
Skip to first unread message

yuanyun.ken

unread,
Oct 30, 2008, 6:16:53 AM10/30/08
to
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.

Roedy Green

unread,
Oct 30, 2008, 6:35:05 AM10/30/08
to
On Thu, 30 Oct 2008 03:16:53 -0700 (PDT), "yuanyun.ken"
<yuany...@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>Do I still need to close stream and how?

see http://mindprod.com/jgloss/exec.html
see sample code for how to close. Scan for "close".
--
Roedy Green Canadian Mind Products
http://mindprod.com
A vote for McCain is fearful clinging to McSame.
A vote for Obama is a shot at Obamalot.

Arne Vajhøj

unread,
Nov 1, 2008, 10:15:26 PM11/1/08
to

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

EJP

unread,
Nov 3, 2008, 2:38:17 AM11/3/08
to
yuanyun.ken wrote:
> 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.

yuanyun.ken

unread,
Nov 4, 2008, 10:56:15 AM11/4/08
to
Thanks for all the great replies , these help me a lot.
0 new messages