Java.lang.Process.waitFor() Method

Advertisements


Description

The java.lang.Process.waitFor() method causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

Declaration

Following is the declaration for java.lang.Process.waitFor() method

public abstract int waitFor()

Parameters

  • NA

Return Value

This method returns the exit value of the process. By convention, 0 indicates normal termination.

Exception

  • NA

Example

The following example shows the usage of lang.Process.waitFor() method.

package com.tutorialspoint;

public class ProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         Process p = Runtime.getRuntime().exec("notepad.exe");

         // cause this process to stop until process p is terminated
         p.waitFor();

         // when you manually close notepad.exe program will continue here
         System.out.println("Waiting over.");

      } catch (Exception ex) {
         ex.printStackTrace();
      }

   }
}

Let us compile and run the above program, this will produce the following result:

Creating Process...
Waiting over.



Advertisements
Advertisements