Is Runtime.exec() ALWAYS Asynchronous?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hal Vaughan

    Is Runtime.exec() ALWAYS Asynchronous?

    I never noticed this before, but when I run an external program through
    Runtime, like this:

    Runtime rt = Runtime.getRunt ime();
    System.out.prin tln("Running external command: " + sCommand);
    try {Process p = rt.exec(sComman d);} catch (Exception e)
    {System.out.pri ntln("Cannot Run Command: " + sCommand + ", Error: " + e);}

    the external command is asynchronous -- completely spun off as a separate
    thread or process. Is there any way to avoid this? I have an external
    command that creates a number of files. Once they're created by the other
    program, I want to use them in my system. Is there a way to wait on a
    Runtime object until the program finishes running? Or a way to check on it
    and see if it is completed?

    I figure I could also add in a loop to wait until the Runtime commands
    create certain files (or alter others), but I can forsee more complications
    with that than with some way to wait until the command is done.

    I searched the Java SDK docs, but can't find methods in Runtime that will
    help.

    Thanks for any help.

    Hal
  • Jonas Kongslund

    #2
    Re: Is Runtime.exec() ALWAYS Asynchronous?

    Hal Vaughan wrote:

    [...][color=blue]
    > Is there a way to wait on a
    > Runtime object until the program finishes running? Or a way to check on
    > it and see if it is completed?[/color]
    [...][color=blue]
    > I searched the Java SDK docs, but can't find methods in Runtime that will
    > help.[/color]

    See the Javadoc for java.lang.Proce ss.

    Process p = Runtime.exec("f oo");
    int exitCode = p.waitFor();

    --
    Jonas Kongslund

    Comment

    • Hal Vaughan

      #3
      Re: Is Runtime.exec() ALWAYS Asynchronous?

      Jonas Kongslund wrote:
      [color=blue]
      > Hal Vaughan wrote:
      >
      > [...][color=green]
      >> Is there a way to wait on a
      >> Runtime object until the program finishes running? Or a way to check on
      >> it and see if it is completed?[/color]
      > [...][color=green]
      >> I searched the Java SDK docs, but can't find methods in Runtime that will
      >> help.[/color]
      >
      > See the Javadoc for java.lang.Proce ss.
      >
      > Process p = Runtime.exec("f oo");
      > int exitCode = p.waitFor();
      >[/color]

      That's exactly what I need. I started w/ Java a few months ago and have
      NEVER used OOP or any kind of programming that deals with classses or
      objects before (I used to program on an Apple //e in Assembler!), so I'm
      still getting used to working with different classes and objects. While it
      makes perfect, simple, sense to check the object created by the command,
      I'm still not used to thinking that way.

      Thanks. This helps with the immediate problem, but also helps me a little
      with getting used to how Java "thinks" and how to use objects.

      Hal

      Comment

      • Amey Samant

        #4
        Re: Is Runtime.exec() ALWAYS Asynchronous?

        Hal Vaughan <hal@thresholdd igital.com> wrote in message news:<DVmpb.913 55$Tr4.252674@a ttbi_s03>...[color=blue]
        > I never noticed this before, but when I run an external program through
        > Runtime, like this:
        >
        > Runtime rt = Runtime.getRunt ime();
        > System.out.prin tln("Running external command: " + sCommand);
        > try {Process p = rt.exec(sComman d);} catch (Exception e)
        > {System.out.pri ntln("Cannot Run Command: " + sCommand + ", Error: " + e);}
        > Hal[/color]

        hi

        Process p = rt.exec(sComman d);
        p. waitFor();
        System.out.prin tln("program exited with : " +p.exitValue()) ;


        is it this you are looking for ?
        this will wait untill your command is finished executing & then you
        can also access its exit value , if required

        regards
        amey

        Comment

        • FISH

          #5
          Re: Is Runtime.exec() ALWAYS Asynchronous?

          Hal Vaughan <hal@thresholdd igital.com> wrote in message news:<DVmpb.913 55$Tr4.252674@a ttbi_s03>...
          [snipped...][color=blue]
          > I searched the Java SDK docs, but can't find methods in Runtime that will
          > help.[/color]


          Process.waitFor () looks like it may do the job.


          -FISH- ><>

          Comment

          Working...