how to write java program get pid [duplicate]-Collection of common programming errors
You can do this using JMX, but beware. The below is not an officially supported mechanism and could change. However, I’ve used this in the past and it works fine.
RuntimeMXBean rmxb = ManagementFactory.getRuntimeMXBean();
System.err.println("pid: " + rmxb.getName());
will print {pid}@hostname