What are the new methods added to Process API in Java 9?

JavaObject Oriented ProgrammingProgramming

Java 9 improves Process class by adding new methods and also provides a new interface: ProcessHandle and ProcessHandle.Info to get all the details about the process and its information.

Below is the list of new methods added to Process in Java 9

  • boolean supportsNormalTermination(): It can return true if the implementation of the destroy() is to normally terminate the process, else returns false.
  • long pid(): It can return the native process ID of the process.
  • ProcessHandle toHandle(): It can return a ProcessHandle for the Process.
  • Stream children(): It can return a snapshot of the direct children of the process.
  • Stream descendants(): It can return a snapshot of the descendants of the process.
  • ProcessHandle.Info info(): It can return a snapshot of information about the process.
  • CompletableFuture onExit(): It can return a CompletableFuture for the termination of the process.

Example

public class ProcessTest {
   public static void main(String args[]) {
      ProcessHandle processHandle = ProcessHandle.current();
      ProcessHandle.Info processInfo = processHandle.info();
      System.out.println(processHandle.pid());
      System.out.println(processHandle.parent());
      System.out.println(processInfo.arguments().isPresent());
      System.out.println(processInfo.command().isPresent());
      System.out.println(processInfo.command().get().contains("tutorialspoint"));
      System.out.println(processInfo.startInstant().isPresent());
   }
}

Output

4892
Optional[7788]
false
true
false
true
raja
Published on 20-Feb-2020 13:47:10
Advertisements