JCMD (Java CoMmanD) Tutorial
This exercise will explain the basic usage of the JDK command line tool jcmd. You can find it in the JDK distribution under JDK_HOME/bin. It will already be on the path if you open the command line interface by double clicking C:\Tutorial\cmd.exe.
Start any Java application. If you already have Eclipse or the stand-alone version of Mission Control running, you are already running one and can skip this step.
Next open a terminal. At the prompt type jcmd and hit enter. Assuming you have jcmd on your path, this will list the running java processes and their Process IDs (PID). If not, either add it to your path, or specify the full path to JDK_HOME/bin/jcmd. Since jcmd uses Java, and it is running, it will list itself as well.
Java Diagnostic Command JCMD
The jcmd uses the PID to identify what JVM to talk to. (It can also use the main class
for identification, but let’s stick with PID for now.) Type **jcmd
Attempt to list the versions of all running JVMs.
JCMD Example Exercises
- Start the Leak program. Use the GC.class_histogram command. Wait for a little while, and then run it again. Can you find any specific use for it?
- You decide that you want your friend to access a running server that has been up for a few days from his computer to help you solve a problem. Oh dear, you didn’t start the external agent when you started the server, did you? Can you find a solution that doesn’t involve taking the server down?
Note: If you want to try the solution without specifying keystores and certificates, make sure you specify jmxremote.ssl=false jmxremote.authenticate=false. Also, specifying a free port is considered good form. Using jmxremote.ssl=false jmxremote.authenticate=false jmxremote.port=4711 should be fine.
- Could you start flight recordings using jcmd? How?
Note: Have you noticed that there is a very similar feature set available from the Diagnostic Commands discussed in Exercise 10 .b and jcmd. As a matter of fact, everything you can do from jcmd you can do using the DiagnosticCommand MBean and vice versa.
Here are some additional videos and articles of mine (Cameron McKenzie) about Java Mission Control and Java Flight Recorder:
- Quick Introduction to Java Mission Control
- Java Mission Control 8 Eclipse plugin Install
- Fix Java memory leaks without costly JVM heap dumps
- The performance cost of autoboxing Java primitive types
- How to start Java's JVM Flight Recorder 3 easy ways
- Performance Problems with Synchronized Methods