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

Discussion in 'Java' started by yuanyun.ken, Oct 30, 2008.

  1. yuanyun.ken

    yuanyun.ken Guest

    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.
    yuanyun.ken, Oct 30, 2008
    #1
    1. Advertising

  2. yuanyun.ken

    Roedy Green Guest

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

    On Thu, 30 Oct 2008 03:16:53 -0700 (PDT), "yuanyun.ken"
    <> 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.
    Roedy Green, Oct 30, 2008
    #2
    1. Advertising

  3. yuanyun.ken

    Arne Vajhøj Guest

    yuanyun.ken wrote:
    > 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
    Arne Vajhøj, Nov 2, 2008
    #3
  4. yuanyun.ken

    EJP Guest

    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.
    EJP, Nov 3, 2008
    #4
  5. yuanyun.ken

    yuanyun.ken Guest

    Thanks for all the great replies , these help me a lot.
    yuanyun.ken, Nov 4, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zsolt Koppany
    Replies:
    2
    Views:
    7,058
    SaintMagoo
    Mar 5, 2011
  2. MARTIN Herve \(EXT\)
    Replies:
    1
    Views:
    1,450
    Anthony Borla
    Nov 27, 2003
  3. Replies:
    4
    Views:
    24,034
    vivek_kothari
    Jan 12, 2010
  4. Xiao Ma
    Replies:
    16
    Views:
    1,400
    =?ISO-8859-15?Q?Arne_Vajh=F8j?=
    Oct 27, 2007
  5. Iñaki Baz Castillo
    Replies:
    7
    Views:
    820
    Iñaki Baz Castillo
    Jan 12, 2010
Loading...

Share This Page