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" );
          }
}

 

0
0
0
s2smodern

W

i

l

d

F

l

y

 

c

h

e

a

t

s

h

e

e

t