Skip to lesson

How the JDK tools fit together

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

Terminal
javac --release 17 FeeReceipt.javajar --create --file fees.jar --main-class FeeReceipt FeeReceipt.classjar --list --file fees.jarjava -jar fees.jar
Sample output
$ 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 FeeReceipt writes a Main-Class: FeeReceipt line into META-INF/MANIFEST.MF, which is why java -jar fees.jar knows what to run. The short old form jar cfe fees.jar FeeReceipt FeeReceipt.class does the same.
  • jar --list shows what is inside. A JAR is a normal zip file, so any zip tool can open it too.

Example 2 · Try code in jshell

Terminal
jshellint fee = 45000 + 18000;fee * 2"PNR-" + 4521/exit
Sample output (JShell from JDK 19)
|  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 ==> 63000 shows the new variable; unnamed results get names such as $2, which you can use in later lines.
  • /exit leaves jshell; /help lists other commands such as /vars and /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

Terminal
javap -cp fees.jar -c FeeReceipt
Sample output (trimmed with ...)
Compiled 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.jar puts the JAR on javap's class path, so it finds FeeReceipt.class inside it. The same works for library JARs, for example to check how a dependency was compiled.
  • Add -v and look at major version to 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

BookingServer.javaJava
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");    }}
Sample output
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

Terminal
jps -ljcmd <pid> VM.versionjcmd <pid> GC.heap_infojstat -gcutil <pid> 1000 3
Sample output (other JVMs trimmed with ...)
$ 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.000

What this code does

  • jps -l lists every JVM you own: the server, jps itself (it is a Java program too) and anything else running, such as your IDE. Replace <pid> with the number from jps or from the program's first line.
  • GC.heap_info shows a G1 heap (the default collector on most machines since Java 9) of 120832 KB with about 3 MB used. In jstat -gcutil, E is how full the young "eden" space is in percent, O is the old space and YGC counts young collections; none has happened yet. The memory modules explain each column.

Example 6 · Record 10 seconds with Java Flight Recorder

Terminal
jcmd <pid> JFR.start duration=10s filename=booking.jfrjfr summary booking.jfr
Sample output (trimmed with ...)
$ 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=10s records for 10 seconds and then writes booking.jfr into the server's working folder. The program keeps running normally while it is recorded.
  • jfr summary counts the events by type. The 10 jdk.ThreadSleep events are the server's 10 one-second sleeps; jdk.ObjectAllocationSample events sample the PNR-... 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.

Terminal
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 command

The 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:class or java -Xlog:class+load to watch classes load.
  • Execution engine and JIT: java -XX:+PrintCompilation to see methods being compiled.
  • Memory and garbage collection: jcmd <pid> GC.heap_info, jstat -gcutil <pid> 1000 and java -Xlog:gc.
  • Leaks and diagnosis: jcmd <pid> GC.class_histogram, GC.heap_dump, Thread.print and JFR recordings.

Next step

Check your understanding

10 quick questions