2011年7月12日 星期二

How a Java Application Can Discover its Process ID (PID)

On UNIX the subclass of Process has a private field with the PID that the default JVM security settings allow you to get via reflection:

  static int getPID(Process process) throws IllegalAccessException, IllegalArgumentException,
      NoSuchFieldException, SecurityException {
    Field field = process.getClass().getDeclaredField("pid");
    field.setAccessible(true);
    return field.getInt(process);
  }


source: http://blog.igorminar.com/2007/03/how-java-application-can-discover-its.html