Importance of destroyForcibly() method in Java 9?

JavaObject Oriented ProgrammingProgramming

The destroyForcibly() method can be used to kill a process. It will be needed if the process has finished or has frozen. For instance, the isAlive() method returns true after destroyForcibly() is called. The destroyForcibly() method returns true if the termination successfully requested, otherwise returns false.

Syntax

boolean destroyForcibly()

In the below example, we will able to launch a notepad application, and it will be terminated after the destroyForcibly() method called.

Example

import java.io.IOException;
import java.lang.ProcessBuilder;

public class DestroyForciblyTest {
   public static void main(String args[]) throws IOException, InterruptedException {
      ProcessBuilder pBuilder = new ProcessBuilder();
      pBuilder.command("notepad.exe");

      // Start notepad application
      Process process = pBuilder.start();
      System.out.println("Started Notepad Application");

      // Sleep for 5 seconds
      Thread.sleep(5000);

      // Kill the notepad
      process.destroyForcibly();
      System.out.println("End of Notepad Application");
   }
}



Output

Started Notepad Application
End of Notepad Application
raja
Published on 30-Apr-2020 20:51:25
Advertisements