When does Process end?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #1

    When does Process end?

    Hi!
    I'm having a problem - I'm calling an external Process with
    [CODE=java]
    Process p = Runtime.getRunt ime().exec("cmd /c ...");
    [/CODE](of course, without the dots but with a command instead) and have to wait for that Process to be completed before progressing. However, if I try waiting with:
    [CODE=java]p.waitFor();[/CODE]or like this:
    [CODE=java]
    boolean finished = false;
    while(!finished )
    {
    try
    {
    p.exitValue();
    finished = true;
    }
    catch(IllegalTh readStateExcept ion itse){}
    }
    [/CODE]it goes into an infinite loop or something. Can anyone at least give me a hint? I'm feeling totally lost right now...

    Greetings,
    Nepomuk
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by nepomuk
    Hi!
    I'm having a problem - I'm calling an external Process with
    [CODE=java]
    Process p = Runtime.getRunt ime().exec("cmd /c ...");
    [/CODE](of course, without the dots but with a command instead) and have to wait for that Process to be completed before progressing. However, if I try waiting with:
    [CODE=java]p.waitFor();[/CODE]or like this:
    [CODE=java]
    boolean finished = false;
    while(!finished )
    {
    try
    {
    p.exitValue();
    finished = true;
    }
    catch(IllegalTh readStateExcept ion itse){}
    }
    [/CODE]it goes into an infinite loop or something. Can anyone at least give me a hint? I'm feeling totally lost right now...

    Greetings,
    Nepomuk
    Read this javaworld article on Runtime.exec pitfalls.

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Originally posted by r035198x
      Read this javaworld article on Runtime.exec pitfalls.
      Thanks for the reply, that article is very interesting, but somehow it won't work nevertheless. My code looks like this now:
      [CODE=java]
      try
      {
      Runtime rt = Runtime.getRunt ime();
      Process proc = rt.exec("cmd /c ...");
      InputStream stderr = proc.getErrorSt ream();
      InputStreamRead er isr = new InputStreamRead er(stderr);
      BufferedReader br = new BufferedReader( isr);
      String line = null;
      System.out.prin tln("<ERROR>");
      while ( (line = br.readLine()) != null)
      System.out.prin tln(line);
      System.out.prin tln("</ERROR>");
      int exitVal = proc.waitFor();
      System.out.prin tln("Process exitValue: " + exitVal);
      }
      catch (Throwable t)
      {
      t.printStackTra ce();
      }
      [/CODE]I basically copied the code from that article and changed the command that it should call. But it still hangs.

      Just in case it's of any importance, the program I'm calling is gunzip and the command works fine without that construct. With the construct, it gets stuck while unpacking after 20KB (of the 9.47MB that it should be).

      Any other ideas?

      Greetings,
      Nepomuk

      *EDIT:* I found, that as soon as I exit my program manually, the extracted file is complete (so it jumps from 20KB to 9.47MB).

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by nepomuk
        Thanks for the reply, that article is very interesting, but somehow it won't work nevertheless. My code looks like this now:
        [CODE=java]
        try
        {
        Runtime rt = Runtime.getRunt ime();
        Process proc = rt.exec("cmd /c ...");
        InputStream stderr = proc.getErrorSt ream();
        InputStreamRead er isr = new InputStreamRead er(stderr);
        BufferedReader br = new BufferedReader( isr);
        String line = null;
        System.out.prin tln("<ERROR>");
        while ( (line = br.readLine()) != null)
        System.out.prin tln(line);
        System.out.prin tln("</ERROR>");
        int exitVal = proc.waitFor();
        System.out.prin tln("Process exitValue: " + exitVal);
        }
        catch (Throwable t)
        {
        t.printStackTra ce();
        }
        [/CODE]I basically copied the code from that article and changed the command that it should call. But it still hangs.

        Just in case it's of any importance, the program I'm calling is gunzip and the command works fine without that construct. With the construct, it gets stuck while unpacking after 20KB (of the 9.47MB that it should be).

        Any other ideas?

        Greetings,
        Nepomuk
        Sorry but which construct are you talking about?

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by r035198x
          Sorry but which construct are you talking about?
          I mean everything I've added. Sorry for being a bit unclear about that.

          Greetings,
          Nepomuk

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            So you mean the problem is that the process is not exiting or is it that the process ends but there is no output produced when it is successful.

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by r035198x
              So you mean the problem is that the process is not exiting or is it that the process ends but there is no output produced when it is successful.
              I can only guess what happens with it while runtime, but as the result is correct, as soon as I quit my program manually (-> the full 9.47MB are there), it seems to have some temporary data somewhere (in a Buffer maybe?) and it puts it in the place it belongs, when it's closed (or it's damn fast as soon as I attempt to close it).

              I just checked and according to the task manager, gunzip runs until I close my program. Am I somehow stopping it from closing?

              Greetings,
              Nepomuk

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by nepomuk
                I can only guess what happens with it while runtime, but as the result is correct, as soon as I quit my program manually (-> the full 9.47MB are there), it seems to have some temporary data somewhere (in a Buffer maybe?) and it puts it in the place it belongs, when it's closed (or it's damn fast as soon as I attempt to close it).

                I just checked and according to the task manager, gunzip runs until I close my program. Am I somehow stopping it from closing?

                Greetings,
                Nepomuk
                You shouldn't try to capture the process' error output stream *and* wait for the
                process to complete in just one thread. Use separate threads for the output
                capturing, the completion of the process (use the join() method) and the final
                return from the outer method call. A bit of wait/notify etc. will do the job.

                kind regards,

                Jos

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by JosAH
                  You shouldn't try to capture the process' error output stream *and* wait for the
                  process to complete in just one thread. Use separate threads for the output
                  capturing, the completion of the process (use the join() method) and the final
                  return from the outer method call. A bit of wait/notify etc. will do the job.

                  kind regards,

                  Jos
                  Thanks, that did the trick.

                  Greetings,
                  Nepomuk

                  Comment

                  • oceatoon
                    New Member
                    • Oct 2007
                    • 1

                    #10
                    Hi
                    I seem to have the same pb, would you mind sharing your final working solution.
                    Thanks in advance

                    Tibor

                    Comment

                    • Nepomuk
                      Recognized Expert Specialist
                      • Aug 2007
                      • 3111

                      #11
                      Originally posted by oceatoon
                      Hi
                      I seem to have the same pb, would you mind sharing your final working solution.
                      Thanks in advance

                      Tibor
                      I'm not able to post the code, as I have no access to it any more (it was for work, but my contract was only for the semester holidays and they are over), but as far as I remember, I basically used 2 new Threads for cleaning the Streams as suggested on this site. (Posted by r035198x in Answer #2.) Look for the StreamGobbler.

                      Greetings,
                      Nepomuk

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by nepomuk
                        I'm not able to post the code, as I have no access to it any more (it was for work, but my contract was only for the semester holidays and they are over), but as far as I remember, I basically used 2 new Threads for cleaning the Streams as suggested on this site. (Posted by r035198x in Answer #2.) Look for the StreamGobbler.

                        Greetings,
                        Nepomuk
                        So you're still alive?
                        I thought you'd gotten kidnapped by terrorists.

                        Comment

                        • Nepomuk
                          Recognized Expert Specialist
                          • Aug 2007
                          • 3111

                          #13
                          Originally posted by r035198x
                          So you're still alive?
                          I thought you'd gotten kidnapped by terrorists.
                          Na, they looked more like aliens from outa space ;-)
                          Just been quite busy lately, but ever so often I come and have a look around here. :-D

                          Greetings,
                          Nepomuk

                          Comment

                          Working...