code: public static int getPid()
        throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
        InvocationTargetException {
      RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
      Field jvm = runtime.getClass().getDeclaredField("jvm");
      jvm.setAccessible(true);
  
      VMManagement mgmt = (VMManagement) jvm.get(runtime);
      Method getProcessId = mgmt.getClass().getDeclaredMethod("getProcessId");
      getProcessId.setAccessible(true);
  
      return (Integer) getProcessId.invoke(mgmt);
    }
docs: Returns the pid of the current process.
  This fails on systems without process identifiers.
  
  @return the processes pid
  @throws NoSuchFieldException if the JVM does not support the VMManagement class
  @throws IllegalAccessException if the JVM does not support the VMManagement class
  @throws NoSuchMethodException if the JVM does not support pids.
  @throws InvocationTargetException if the JVM does not support pids.