Sunday, January 16, 2011

Java snippet: get PID at runtime



public class MyPid {
  public static void main(String[] args) {
    String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
    int p = nameOfRunningVM.indexOf('@');
    String pid = nameOfRunningVM.substring(0, p);
    System.out.println("Java app PID: " + pid);

1 comment:

ramesgh said...

Hi Anton,

Thanks for your posting, i tried this code. it will execute fine and also i got different PID, when i execute again.I have one doubt, the PID of USB drive will change at again and again while its run. Is it correct. I'm new for this..

Disqus for Code Impossible