Thursday, May 9, 2013

How to get process ID of current JVM

import java.lang.management.ManagementFactory;

public class ProcessID {
   public static void main(String[] args) {
      getProcessId();
   }

   public static String getProcessId() {
      String pname = ManagementFactory.getRuntimeMXBean().getName();
      System.out.println("process name = " + pname);
      String pid = pname;
      int i = pname.indexOf("@");
      if (i!=-1) pid = pname.substring(0,i);
      System.out.println("process id = " + pid);
      return pid;
   }

}


Output:

process name = 7108@KH1205
process id = 7108

No comments:

Post a Comment