code: public static int pid() {
          int localPID = 0;
          try {
              String name = ManagementFactory.getRuntimeMXBean().getName();
              String[] nameSplit = name.split("@");
              if(nameSplit.length > 1) {
                  localPID = Integer.parseInt(nameSplit[0]);
              }
              return localPID;
          } catch(Throwable t) {
              throw new UnsupportedOperationException("An error occurred while getting the PID: " + t.getMessage()) ;
          }
      }
docs: Return the PID from the currently running JVM or 0 if it could not be
  retrieved. This should work on most *nix JVM implementations.