How the JDK tools fit together
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A JDK is more than javac and java. Its bin folder holds tools for three jobs: build and run a program (javac, java, jar, jshell), inspect compiled code (javap), and watch a running JVM (jps, jcmd, jstat and Java Flight Recorder).
Build and run: javac, java, jar, jshell
javac compiles and java runs, as you know. jar packs class files (and other files such as images) into one JAR file (Java ARchive), a zip file with a META-INF/MANIFEST.MF inside.
Example 1 · Package and run a JAR
javac --release 17 FeeReceipt.javajar --create --file fees.jar --main-class FeeReceipt FeeReceipt.classjar --list --file fees.jarjava -jar fees.jar$ jar --create --file fees.jar --main-class FeeReceipt FeeReceipt.class $ jar --list --file fees.jar META-INF/ META-INF/MANIFEST.MF FeeReceipt.class $ java -jar fees.jar Receipt for Riya Sharma Tuition: Rs 45000 Hostel: Rs 18000 Total: Rs 63000
What this code does
--main-class FeeReceiptwrites aMain-Class: FeeReceiptline intoMETA-INF/MANIFEST.MF, which is whyjava -jar fees.jarknows what to run. The short old formjar cfe fees.jar FeeReceipt FeeReceipt.classdoes the same.jar --listshows what is inside. A JAR is a normal zip file, so any zip tool can open it too.
Example 2 · Try code in jshell
jshellint fee = 45000 + 18000;fee * 2"PNR-" + 4521/exit| Welcome to JShell -- Version 19.0.2 | For an introduction type: /help intro jshell> int fee = 45000 + 18000; fee ==> 63000 jshell> fee * 2 $2 ==> 126000 jshell> "PNR-" + 4521 $3 ==> "PNR-4521" jshell> /exit | Goodbye
What this code does
- No class, no
main, no semicolon needed for an expression.fee ==> 63000shows the new variable; unnamed results get names such as$2, which you can use in later lines. /exitleaves jshell;/helplists other commands such as/varsand/methods. The version banner shows which JDK's jshell you started.
With --main-class, the manifest records which class has main, so java -jar fees.jar can run it.
jshell (Java 9 and later) is an interactive shell, a REPL (read, evaluate, print loop). You type a line of Java and see the result at once, with no class and no main. It is the quickest way to test what "PNR-" + 4521 gives or how a method behaves.
Inspect: javap
javap disassembles compiled classes, as the javap lesson showed. It reads classes from anywhere on its class path, including JAR files: javap -cp fees.jar -c FeeReceipt disassembles a class inside a JAR without unzipping it.
Example 3 · javap on a class inside a JAR
javap -cp fees.jar -c FeeReceiptCompiled from "FeeReceipt.java"
public class FeeReceipt {
public FeeReceipt();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: ldc #7 // String Riya Sharma
2: astore_1
...What this code does
-cp fees.jarputs the JAR on javap's class path, so it findsFeeReceipt.classinside it. The same works for library JARs, for example to check how a dependency was compiled.- Add
-vand look atmajor versionto see which Java a library JAR needs before you deploy it to an older server.
Use it when the question is "what did javac really produce?": which invoke a call uses, whether boxing happens, or which class file version a library was built for. It never runs the code.
Watch a running JVM: jps, jcmd, jstat and JFR
These tools attach to a JVM that is already running, on the same computer and as the same user. jps lists running JVMs with their process id (pid), the number the operating system gives each running program.
Example 4 · A small server to watch
import java.util.ArrayList;import java.util.List;
public class BookingServer { public static void main(String[] args) throws InterruptedException { System.out.println("Booking server started, pid " + ProcessHandle.current().pid()); System.out.println("Running for 60 seconds. Try jps, jcmd and jstat now.");
List<String> recent = new ArrayList<>(); for (int second = 1; second <= 60; second++) { for (int i = 0; i < 1000; i++) { recent.add("PNR-" + second + "-" + i); // some garbage every second } if (recent.size() > 5000) { recent.clear(); } Thread.sleep(1000); } System.out.println("Booking server stopped"); }}Booking server started, pid 26208 Running for 60 seconds. Try jps, jcmd and jstat now. Booking server stopped
What this code does
ProcessHandle.current().pid()(Java 9) prints the program's process id, the number the tools need. Your pid will differ on every run.- The program runs for 60 seconds and makes a little garbage every second, so there is something to watch. Run it in one terminal and the commands of the next examples in a second terminal.
Example 5 · jps, jcmd and jstat on the running server
jps -ljcmd <pid> VM.versionjcmd <pid> GC.heap_infojstat -gcutil <pid> 1000 3$ jps -l
26208 BookingServer
28432 jdk.jcmd/sun.tools.jps.Jps
...
$ jcmd 26208 VM.version
26208:
OpenJDK 64-Bit Server VM version 21.0.7+9-b895.130
JDK 21.0.7
$ jcmd 26208 GC.heap_info
26208:
garbage-first heap total 120832K, used 2990K [0x000000008a600000, 0x0000000100000000)
region size 1024K, 3 young (3072K), 0 survivors (0K)
Metaspace used 493K, committed 640K, reserved 1114112K
class space used 33K, committed 128K, reserved 1048576K
$ jstat -gcutil 26208 1000 3
S0 S1 E O M CCS YGC YGCT FGC FGCT CGC CGCT GCT
- - 8.70 0.00 - - 0 0.000 0 0.000 0 0.000 0.000
- - 8.70 0.00 - - 0 0.000 0 0.000 0 0.000 0.000
- - 8.70 0.00 - - 0 0.000 0 0.000 0 0.000 0.000What this code does
jps -llists every JVM you own: the server,jpsitself (it is a Java program too) and anything else running, such as your IDE. Replace<pid>with the number fromjpsor from the program's first line.GC.heap_infoshows a G1 heap (the default collector on most machines since Java 9) of 120832 KB with about 3 MB used. Injstat -gcutil,Eis how full the young "eden" space is in percent,Ois the old space andYGCcounts young collections; none has happened yet. The memory modules explain each column.
Example 6 · Record 10 seconds with Java Flight Recorder
jcmd <pid> JFR.start duration=10s filename=booking.jfrjfr summary booking.jfr$ jcmd 26208 JFR.start duration=10s filename=booking.jfr 26208: Started recording 1. The result will be written to: C:\...\l6\booking.jfr $ jfr summary booking.jfr Version: 2.1 Chunks: 1 Start: 2026-10-15 18:08:25 (UTC) Duration: 10 s Event Type Count Size (bytes) ============================================================= jdk.BooleanFlag 500 14740 jdk.ModuleExport 456 3975 jdk.SystemProcess 418 22917 ... jdk.ThreadSleep 10 180 jdk.CPULoad 7 126 jdk.ClassLoadingStatistics 7 63 jdk.GCHeapMemoryPoolUsage 6 213 jdk.ObjectAllocationSample 4 52 ...
What this code does
JFR.start duration=10srecords for 10 seconds and then writesbooking.jfrinto the server's working folder. The program keeps running normally while it is recorded.jfr summarycounts the events by type. The 10jdk.ThreadSleepevents are the server's 10 one-second sleeps;jdk.ObjectAllocationSampleevents sample thePNR-...strings it creates. Later modules read GC and allocation events from recordings like this.
jcmd sends a diagnostic command to one JVM: its version, a heap summary, a thread dump, a heap dump, or starting a recording. jstat prints memory and garbage collection statistics again and again at a fixed interval.
Java Flight Recorder (JFR) records events inside the JVM, such as garbage collections, allocations, thread sleeps and CPU load, into a .jfr file with low overhead. Start a recording with jcmd <pid> JFR.start and read it with the jfr tool (or a viewer such as JDK Mission Control). JFR has been free in OpenJDK since Java 11.
jcmd replaces several older tools: jcmd <pid> Thread.print does what jstack does, and jcmd <pid> GC.class_histogram does what jmap -histo does.Finding the tools, their help, and where they come next
All these tools live in the JDK's bin folder, next to java and javac; if javac works from your terminal, they do too. Every tool prints its options with --help or -h.
ls "$JAVA_HOME/bin" # macOS/Linux; Windows: dir "%JAVA_HOME%\bin"javac --help # every tool has --help or -hjcmd -l # same list as jpsjcmd <pid> help # commands this JVM supportsjcmd <pid> help GC.heap_info # help for one commandThe commands below list the tools, ask for help and show what a running JVM supports.
jcmd <pid> help lists the commands that particular JVM supports.
- Class loading:
java -verbose:classorjava -Xlog:class+loadto watch classes load. - Execution engine and JIT:
java -XX:+PrintCompilationto see methods being compiled. - Memory and garbage collection:
jcmd <pid> GC.heap_info,jstat -gcutil <pid> 1000andjava -Xlog:gc. - Leaks and diagnosis:
jcmd <pid> GC.class_histogram,GC.heap_dump,Thread.printand JFR recordings.
Next step
Check your understanding
10 quick questions