Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Language Features by Version

JDK command-line tools

A tour of the tools in the JDK bin folder: java, javac, jar, javadoc, jshell, jlink, jpackage, jdeps, jcmd, jfr and jar signing, with real command lines.

The JDK (Java Development Kit) ships a set of command-line programs in its bin folder. Most Java developers meet java and javac first, then rely on IDEs and build tools for everything else, but each tool is worth knowing because Maven, Gradle and your IDE call them underneath. Run any tool with --help, and see the manual with man on Linux and macOS.

The sandbox on this page cannot launch these programs, so the command lines below are read-only. Two of the tools exist as APIs inside the JDK, and the runnable examples use them as stand-ins: the compiler (javax.tools) and the JAR format (java.util.jar).

Everyday tools

Terminal
# Read-only
java -version                          # which JDK am I running?
javac -d out src/com/shop/*.java       # compile into the folder "out"
java -cp out com.shop.Main             # run a class from a class path
java Hello.java                        # compile in memory and run one source file
jshell                                 # interactive REPL (see the JShell page)
jar --create --file app.jar --main-class com.shop.Main -C out .
java -jar app.jar
Tool Purpose
java Launches a class, a JAR (-jar), a module (-m) or a single source file
javac Compiles .java files to .class files; --release N targets an older platform API
jar Creates, lists and extracts JAR (ZIP) archives, sets the manifest and main class
javadoc Generates HTML API documentation from /** ... */ comments
jshell Evaluates snippets without a class or main method
jdeps Analyzes dependencies between classes, packages and modules
jlink Builds a small custom runtime containing only the modules you need
jpackage Creates a native installer (MSI, DMG, DEB, RPM) or app image that bundles a runtime
jcmd Sends commands to a running JVM: thread dumps, heap dumps, flags, JFR
jfr Prints and summarizes Java Flight Recorder files
jstack, jmap, jstat, jinfo Older single-purpose inspection tools; jcmd covers most of them
keytool, jarsigner Manage key stores and certificates; sign and verify JARs
javap Disassembles class files to show bytecode and members
jmod, jimage Work with JMOD files and the runtime image format

Compiler options you will use

Terminal
# Read-only
javac --release 21 Main.java              # compile against Java 21's API, not just the syntax
javac -Xlint:all -Werror Main.java         # all warnings, treat them as errors
javac -parameters Main.java                # keep parameter names for reflection
javac --enable-preview --release 25 Main.java
javac -processor-path libs -proc:full ...  # annotation processing; implicit processing is off by default from Java 23
javap -c -p Main.class                     # show the bytecode of every method

From program to runtime image

Terminal
# Read-only: package a modular application with its own trimmed JRE
jdeps --print-module-deps --ignore-missing-deps app.jar
jlink --module-path mods --add-modules com.shop.app,java.sql --output runtime --strip-debug --compress zip-6
runtime/bin/java -m com.shop.app/com.shop.Main

jpackage --input libs --main-jar app.jar --main-class com.shop.Main --name Shop --type deb

jlink needs modular input and is how you get a runtime of 30 to 60 MB instead of a full JDK. jpackage calls jlink or accepts a runtime image and wraps it with a launcher, icons and an installer. It must run on the operating system it targets.

Diagnosing a running program

Terminal
# Read-only
jcmd                                   # list running Java processes
jcmd 4242 Thread.print                 # thread dump
jcmd 4242 GC.heap_dump /tmp/heap.hprof
jcmd 4242 VM.flags
jcmd 4242 JFR.start duration=60s filename=rec.jfr
jfr summary rec.jfr
jfr print --events jdk.GarbageCollection rec.jfr

Try it

Java A compiler inside the JDK (javac stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Creating and reading a JAR (jar stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Which JDK modules and tools are present?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Usage notes

  • javac --release N beats -source N -target N. It also checks that you use only APIs that existed in release N, while -source/-target alone allow newer APIs and fail at run time on the older JVM.
  • The java launcher compiles for you when its first argument ends in .java. Classes in a single-file program cannot be seen by other source files, and a missing main is an error. Pass extra classes with --class-path.
  • JAR files are ZIP files. You can open them with any ZIP tool. The META-INF/MANIFEST.MF file holds Main-Class for java -jar, Class-Path for neighbors and Multi-Release: true for version-specific classes.
  • jlink and jpackage need compatible modules. Automatic modules (plain JARs on the module path) cannot be linked. Use jdeps --generate-module-info to get started.
  • Since JDK 23, annotation processing needs -proc:full (or an explicit processor option) when you compile; before that, processors found on the class path ran implicitly.
  • Tools are versioned with the JDK. Run javac -version and java -version in the same shell to catch the common mix-up of a newer compiler and an older runtime (UnsupportedClassVersionError).
  • Java 11 and later can run without a separate JRE. Oracle stopped shipping a standalone JRE; build a runtime with jlink or use a distribution that offers a JRE package.