Advertise with us

Our eBooks

Getting the process id (PID) programmatically in Java 9

Java 9 features a simple an portable way to retrieve the process id of the Java Virtual Machine. This can be done through the ProcessHandle which identifies and provides control of native processes. Each individual process can be monitored for liveness, list its children, get information about the process or destroy it. By comparison, Process instances were started by the current process and additionally provide access to the process input, output, and error streams.

package com.demo;
      
public class GetProcessId {
      
          public ProcessIdDescriptor getPid() {
              long pid = ProcessHandle.current().getPid();
              return new ProcessIdDescriptor( pid, "ProcessHandle" );
          }
}

 

© 2021 mastertheboss.com. All Rights Reserved.

Please publish modules in offcanvas position.