How to get a snapshot of information about Process API in Java 9?

JavaObject Oriented ProgrammingProgramming

Java 9 has improved Process API by including new methods and introduced new interfaces ProcessHandle and ProcessHandle.Info to get all the details regarding the process and its information.

ProcessHandle interface can identify and provide control of native processes. Each individual process can be monitored for liveness, listed its children, get information about the process, or destroys it. ProcessHandle.Info interface gives information snapshots about a process.

Syntax

ProcessHandle.Info info()

Example

public class ProcessSnapShotTest {
   public static void main(String[] args) {
      ProcessHandle currentProcessHandleImpl = ProcessHandle.current();
      
      // Process snapshot of the current running process with ProcessHandle.Info:
      ProcessHandle.Info processInfo = currentProcessHandleImpl.info();

      System.out.println("nProcess snapshot of the current running process:");
      System.out.println("User : " + processInfo.user().get());
      System.out.println("Start Time : " + processInfo.startInstant().get());
   }
}

Output

Process snapshot of the current running process:
User : Tutorialspoint\User
Start Time : 2020-05-01T05:44:41.458Z
raja
Published on 01-May-2020 12:03:51
Advertisements