Share this page 

Get the PID of the current Java process(pure Java solution)Tag(s): Environment

With JDK9,
  public static long getCurrentPid() {
     ProcessHandle processHandle = ProcessHandle.current();
With previous JDK version, the RuntimeMXBean can be used. The name of the bean contains the pid (ex. 12345@localhost).

Warning : The returned name string can be any arbitrary string and a Java virtual machine implementation can choose to embed platform-specific useful information in the returned name string.

On the Sun JVM (Windows plateform), the PID is present.

public class SystemUtils {

  private SystemUtils() {}

  public static long getPID() {
    String processName =;
    return Long.parseLong(processName.split("@")[0]);

  public static void main(String[] args) {
    String msg = "My PID is " + SystemUtils.getPID();

        null, msg, "SystemUtils", javax.swing.JOptionPane.DEFAULT_OPTION);


The result is

For a Java-JNI solution, see this HowTo.