Java.lang.Process.destroy() Method

Advertisements


Description

The java.lang.Process.destroy() method kills the subprocess. The subprocess represented by this Process object is forcibly terminated.

Declaration

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

public abstract void destroy()

Parameters

  • NA

Return Value

This method does not return a value.

Exception

  • NA

Example

The following example shows the usage of lang.Process.destroy() 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");

         // wait 10 seconds
         System.out.println("Waiting...");
         Thread.sleep(10000);

         // kill the process
         p.destroy();
         System.out.println("Process destroyed.");

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

   }
}

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

Creating Process...
Waiting...
Process destroyed.



Advertisements
Advertisements