What is the importance of the jcmd tool in Java 9?

JavaObject Oriented ProgrammingProgramming

The "jcmd" is JVM diagnostic tool, which is a command-line tool to run diagnostic commands against given JVM on the local machine. This tool has been included in the JDK installation since Java 7 version, and it can be represented by the "%java_home%\bin\jcmd.exe" program file. If we have "%java_home%\bin" directory included in "path" the environment variable, we can run "jcmd -h" command to see a complete list of all options as below

C:\Users\User>jcmd -h
Usage: jcmd
   or: jcmd -l
   or: jcmd -h

   command must be a valid jcmd command for the selected jvm.
   Use the command "help" to see which commands are available.
   If the pid is 0, commands will be sent to all Java processes.
   The main class argument will be used to match (either partially
   or fully) the class used to start Java.
   If no options are given, lists Java processes (same as -l).

   PerfCounter.print display the counters exposed by this process
   -f read and execute commands from the file
   -l list JVM processes on the local machine
   -h this help

Example

public class JCmdToolTest {
   public static void main(String args[]) {
      Runtime runtime = Runtime.getRuntime();
      System.out.println("Free memory: " + runtime.freeMemory());
      System.out.println("Total memory: " + runtime.totalMemory());
      try {
         Thread.sleep(5000);
      } catch(InterruptedException e) {
      }
   }
}

Output

Free memory: 65454560
Total memory: 67108864


We can use the "jcmd -l" command to list all running JVMs on the local machine, then use the PID or the class main from the output to identify the JVM.

C:\Users\User>jcmd -l
6108 jdk.jcmd/sun.tools.jcmd.JCmd -l
raja
Published on 29-Apr-2020 17:48:03
Advertisements