Sunday, May 6, 2012

How to get JVM PID(Process ID) from Java Program.

As you all know each and every process running in the Operation System has an unique ID. In Unix/Linux it is easy to find the PID from java using Runtime class, but in Windows, earlier versions of Java it difficult to get the PID of the JVM. In Java5.0, it is easy to find the OS Process ID assigned to the JVM.
There is an utility that is ment to return the PID of all running JVM on the machine or remote machine. This utility is not only ment for PID, you can also get the argument and complete package name.

The good thing with this tool is you can monitor any JVM on the network just you have to have the IP of that machine.

Let's discuss about the utility.

In the binary folder you can find an executable file named "jps.exe". The complete name is "JVM Process Status tool", this utility takes some parameters and work as per that option.

Syntax:

jps [options] [hostid]

Here options is the list of options and hostid is the IP of the remote machine for which you want to monitor.

"jps" options are listed below:
  • -l Output the full package name for the application's main class or the full path name to the application's JAR file.
  • -m Output the arguments passed to the main method. The output may be null for embedded JVMs.
  • -q Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM identifiers.
  • -v Output the arguments passes to the JVM.
  • -help Returns the syntax of this tool.

You don't need to use the "hostid" command argument, if you want to list JVM processes on the local machine. But to list JVM processes on a remote, you need to use "hostid" to specify how to connect to the remove machine.

Getting Help : jps.exe -help

Getting only PID : jps.exe -q

Getting only main Class name : jps.exe -m

Getting with complete package name : jps.exe -l

Getting with argument : jps.exe -v

Hope this program will helps you to understand the utility.