Java Tools Tutorials - Herong's Tutorial Examples - v6.24, by Herong Yang
jcmd - JVM Diagnostic Tool Command
This section describes what is 'jcmd' (Java jcmd Tool) - is a command line tool to run diagnostic commands against a given JVM on the local machine.
What Is "jcmd"? - "jcmd", The JVM Diagnostic Tool, is a command line tool to run diagnostic commands against a given JVM on the local machine.
"jcmd" has been included in JDK installation since JDK 7. And it is represented by the %java_home%\bin\jcmd.exe program file.
If you have %java_home%\bin directory included in "path" the environment variable, you can run "jcmd -h" to see a complete list of all options:
herong> jcmd -h Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file> 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
Let's try with the LongSleep.java program:
/* LongSleep.java * Copyright (c) 2005 HerongYang.com. All Rights Reserved. */ class LongSleep { public static void main(String[] a) { Runtime rt = Runtime.getRuntime(); System.out.println(" Free memory: " + rt.freeMemory()); System.out.println("Total memory: " + rt.totalMemory()); try {Thread.sleep(1000*60*60);} catch (InterruptedException e) {} } }
Compile and run it:
herong> javac LongSleep.java herong> javac LongSleep
Now 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.
herong> jcmd -l 10848 LongSleep 8352 jdk.jcmd/sun.tools.jcmd.JCmd -l
The output tells us that I have a JVM running my LongSleep.java program on my local machine. We can identify that JVM with the PID of 10848, or the class name of "LongSleep".
By the way, you can also use the "jps -l" command to get the same information as "jcmd -l":
herong> jps -l 10848 LongSleep 4344 jdk.jcmd/sun.tools.jps.Jps
For more information, see "jcmd" reference page at https://docs.oracle.com/en/java/javase/17/docs/specs/man/jcmd.html.
Table of Contents
javac - The Java Program Compiler
java - The Java Program Launcher
jpackage - Binary Package Builder
javadoc - The Java Document Generator
jdeps - The Java Class Dependency Analyzer
jdeprscan - The Java Deprecated API Scanner
►jcmd - The JVM Diagnostic Tool
►jcmd - JVM Diagnostic Tool Command
"jcmd 0 help" - Listing JVM Diagnostic Commands
"jcmd 0 VM.*" - Running VM Diagnostic Commands
"jcmd 0 GC.*" - Running GC Diagnostic Commands
"jcmd 0 Thread.print" - Generating Thread Dump
jconsole - Java Monitoring and Management Console
jstat - JVM Statistics Monitoring Tool
jhsdb - The Java HotSpot Debugger
jvisualvm (Java VisualVM) - JVM Visual Tool
javap - The Java Class File Disassembler
keytool - Public Key Certificate Tool
jrunscript - Script Code Shell
native2ascii - Native-to-ASCII Encoding Converter