Java Process waitFor() Method



Description

The Java 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

Making a Notepad Process to Wait Example

The following example shows the usage of Process destroy() method. We've created a Process object for notepad executable. Then we've kept process to wait and a message is printed.

package com.tutorialspoint;

public class ProcessDemo {

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

         // 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();
      }
   }
}

Output

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

Creating Process...
Waiting over.

Making a Calculator Process to Wait Example

The following example shows the usage of Process destroy() method. We've created a Process object for calculator executable. Then we've kept process to wait and a message is printed.

package com.tutorialspoint;

public class ProcessDemo {

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

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

         // when you manually close calc.exe program will continue here
         System.out.println("Waiting over.");
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Creating Process...
Waiting over.

Making a Windows Explorer Process to Wait Example

The following example shows the usage of Process destroy() method. We've created a Process object for windows explorer executable. Then we've kept process to wait and a message is printed.

package com.tutorialspoint;

public class ProcessDemo {

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

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

         // when you manually close explorer.exe program will continue here
         System.out.println("Waiting over.");
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Creating Process...
Waiting over.
java_lang_process.htm
Advertisements