I face a process handling problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    I face a process handling problem

    I run process on unix through runtime.exec() method.
    But my process termintaes before chid process terminates.
    How can I trap that child process termination.
    The process.waitfor method block for while until the process terminates..
    Can anyone tell me how can I get process information randomly until the main process terminates and it's child processes also throgh java..
    Plz send me the solution immediately
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    #2
    If you need a Java answer then I'd ask in the Java board, otherwise, I can only give a C answer after I look up a detail I forgot.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      Originally posted by dmjpro
      I run process on unix through runtime.exec() method.
      But my process termintaes before chid process terminates.
      How can I trap that child process termination.
      The process.waitfor method block for while until the process terminates..
      Can anyone tell me how can I get process information randomly until the main process terminates and it's child processes also throgh java..
      Plz send me the solution immediately
      this starts a child process and receives and displays its output - is that what you require?
      Code:
      // execute a child process using java exec command and get output
      
      import java.io.*;
      import java.lang.*;
      
      public class JavaExec {
      
      public static void main (String args[]){
        try {
           // get runtime environment and execute child process
           Runtime systemShell = Runtime.getRuntime();
           Process output = systemShell.exec("java -jar hello.jar");
           // open reader to get output from process
           BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
           String line = null;
           System.out.println("<OUTPUT/>");
            while((line = br.readLine()) != null ) 
               { System.out.println(line);  }          // display process output
           System.out.println("</OUTPUT>");
           int exitVal = output.waitFor();             // get process exit value
           System.out.println("Process Exit Value : "+ exitVal);
           }
         catch (IOException ioe){ System.err.println(ioe); }
         catch (Throwable t) { t.printStackTrace();}
      }
      }

      Comment

      Working...