Here is a simple tip for Java developers. Let’s say if you had to find the process id (pid) of a running Java process within your Java application, following is the code for that.

	import sun.management.VMManagement;
	import java.lang.management.ManagementFactory;
	import java.lang.management.RuntimeMXBean;
	import java.lang.reflect.Field;
	import java.lang.reflect.Method;

	public class FindJavaProcessId {


		public static void main(String[] args) {

			try {
				RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
				Field jvmField = runtimeMXBean.getClass().getDeclaredField("jvm");
				jvmField.setAccessible(true);
				VMManagement vmManagement = (VMManagement) jvmField.get(runtimeMXBean);
				Method getProcessIdMethod = vmManagement.getClass().getDeclaredMethod("getProcessId");
				getProcessIdMethod.setAccessible(true);
				Integer processId = (Integer) getProcessIdMethod.invoke(vmManagement);
				System.out.println("################    ProcessId = " + processId);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

Well, where do I use it? Just about anywhere. Let’s say if you had to use ProcessExplorer or Unix/Linux based lsof command to track/monitor running JVM process, it would be handy if you know what the process id is. You can also argue that I can always find that through the list of process ids either from ps command or from ProcessExplorer. What if you have multiple JVM processes with the same command line arguments? Well then, knowing the exact pid of the process is really helpful :) . I think its a good practice to print the pid along with other arguments like classpath, runtime context etc. during startup of the applications like application server, container, daemon console etc. so that we can use that later.

Please take a note on line 1, which means this code will run only on Sun’s JRE. Yes, it is not a good practice to write application code based on JDK’s internal packages. But, this is an exceptional scenario like monitoring the application itself so, I don’t see why not. And, I tested only in Java version 1.6 in Windows & Linux. I am sure there should be an equivalent code for other JDK’s also.

5 thoughts on “Java Tip: Find process id of running Java process

  1. Pingback: Java Tip: Find process id of running Java process

  2. Pingback: Java Tip: Find process id of running Java process « turtle9

  3. Hi,
    with which method in this instruction u start this instruction
    vmManagement.getClass().getDeclaredMethod(“getProcessId”);
    there’s no “getProcessId” in all the code program ???

  4. This is very simple code to get JVM process id.
    String name = ManagementFactory.getRuntimeMXBean().getName();
    int index = name.indexOf(‘@’);
    Long processId = Long.parseLong(name.substring(0.index));
    System.out.println(“Process ID : “+processId);

    Enjoy

  5. The method doesn’t work when VMManagement is unavailable due to usage restrictions on rt.jar, which is the case in my environment. Not sure why the restrictions are there or how to get around them, so some other approach is needed. The RuntimeMXBean name might work, as Sawan suggests, since it isn’t using a restricted class.

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>