Install a JDK on Linux, macOS and Windows

A JDK is one installation with one folder, and every command of this course comes from it. There is nothing to configure before the first program: the package manager puts java and javac on the path, and the only real choice is which JDK you install and which version it is.

The course is written and tested with Java 21, the current long-term-support release. A newer JDK runs every program on these pages, and a page says so when a line of output depends on the version; a JDK older than 21 does not build the lessons that use records, sealed types and pattern matching.

One package per system

On Linux, the distribution package is the shortest way. Debian and Ubuntu have openjdk-21-jdk, Fedora and RHEL have java-21-openjdk-devel, Arch has jdk21-openjdk, and Alpine has openjdk21. The package that ends in -jdk or -devel is the one with the compiler; a package named only jre or headless runs programs but cannot build them.

On macOS, the Temurin build from the Eclipse Adoptium project is a signed installer for both Apple silicon and Intel, and Homebrew has openjdk@21. On Windows, the same Temurin project ships an MSI and a zip; the MSI sets the path and the zip does not. The install guide has a page per system, with the exact package names and the commands: Linux, macOS, FreeBSD and OpenBSD.

Verify the installation

Two commands answer the question, and they must agree:

java -version
javac -version
command -v java
command -v javac

openjdk version "21.0.13.1" 2026-11-17
OpenJDK Runtime Environment (build 21.0.13.1+2-alpine-r0)
OpenJDK 64-Bit Server VM (build 21.0.13.1+2-alpine-r0, mixed mode, sharing)
javac 21.0.13.1
/usr/bin/java
/usr/bin/javac

java -version writes three lines and javac -version writes one. If the first command works and the second says command not found, a JRE is installed and the JDK is not; if the two report different versions, two JDKs are installed and the path picks one of them for each command, which is a problem worth fixing before it produces a class file the other JVM refuses.

JAVA_HOME and PATH

Most build tools read one environment variable, JAVA_HOME, and expect it to name the folder that holds the JDK, not the bin folder inside it. The package on this machine sets it, and the JVM reports the same folder as the java.home property:

echo "JAVA_HOME=$JAVA_HOME"

JAVA_HOME=/usr/lib/jvm/java-21-openjdk

When the variable is empty, set it in the shell profile and put its bin folder first on the path. On Linux and macOS that is a line in ~/.profile or ~/.zshrc; on Windows it is an entry in the environment variables dialog. Then open a new terminal, because a running shell keeps the old environment:

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk
export PATH="$JAVA_HOME/bin:$PATH"

The order matters. A bin folder later on the path is only used when no earlier folder has the command, so an old JRE in /usr/bin wins over a newer JDK in /opt unless the JDK comes first.

The version, in a program

Runtime.version() returns the version as an object with four parts: feature, interim, update and patch. Java 21.0.13.1 has feature 21, interim 0, update 13 and patch 1. The program below prints the parts and the two properties that say where the JDK is and which class file version it writes.

The file Version.java:

public class Version { public static void main(String[] args) { Runtime.Version version = Runtime.version(); System.out.println("feature: " + version.feature()); System.out.println("interim: " + version.interim()); System.out.println("update: " + version.update()); System.out.println("java.home: " + System.getProperty("java.home")); System.out.println("class version: " + System.getProperty("java.class.version")); System.out.println("file.encoding: " + System.getProperty("file.encoding")); } }

javac -Xlint:all Version.java
java Version

feature: 21
interim: 0
update: 13
java.home: /usr/lib/jvm/java-21-openjdk
class version: 65.0
file.encoding: UTF-8

java.home is the folder the running JVM came from, and it is the same one JAVA_HOME names here. java.class.version is the class file version this JVM understands: 65.0 for Java 21, 61.0 for Java 17, 55.0 for Java 11. When a build fails with UnsupportedClassVersionError, the number in the message is this one, and the compiler that wrote the file was newer than the JVM that runs it.

The class file version

javac --release 17 writes a class file for Java 17 while the compiler itself is 21. That is how a library stays usable on an older runtime. The header changes, and the program still runs on the 21 JVM, which accepts every older class file:

javac --release 17 -Xlint:all -d r17 Version.java
od -An -tx1 -N8 r17/Version.class
java -cp r17 Version

ca fe ba be 00 00 00 3d
feature: 21
interim: 0
update: 13

0x3d is 61, the class file version of Java 17, and the three lines after it are the same as before: the feature() call reports the runtime that executes the class, not the release the class was compiled for. A class file is a promise about syntax and API, not about the machine that will run it.

Exercises

  1. Run the two version commands on your machine. Do they name the same JDK?
  2. Print System.getProperty("java.home") and compare it with echo $JAVA_HOME.
  3. Compile Version.java with --release 17 and with no flag. Compare the two class files byte by byte in the first eight bytes.
  4. Find the bin folder of your JDK and run ./javac -version from inside it. Which version does it report, and does it depend on the path?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. java -version works and javac -version says command not found. What is installed?

    Answer

    A JRE, without the compiler

    The runtime runs programs and the compiler builds them; a package without javac is a JRE.

  2. What should JAVA_HOME name?

    Answer

    The folder of the JDK, the parent of bin

    Build tools append bin to JAVA_HOME, so the variable names the folder that holds bin.

  3. Which class file version does a Java 21 compiler write by default?

    Answer

    65.0

    The class file version of Java 21 is 65.0; the language version and the class file version are different numbers.

  4. What does this program print? public class Q1 { public static void main(String[] args) { Runtime.Version v = Runtime.version(); System.out.println(v.feature() + "." + v.update()); } }

    Answer

    21.13

    feature() is 21 and update() is 13, so the two joined with a dot are 21.13.

  5. What does this program print? public class Q2 { public static void main(String[] args) { System.out.println(System.getProperty("java.class.version")); } }

    Answer

    65.0

    java.class.version is the class file version of the running JVM, 65.0 for Java 21.

  6. A class file built by a Java 21 javac is run on a Java 17 JVM. What happens?

    Answer

    The JVM refuses it with UnsupportedClassVersionError

    A JVM accepts class files up to its own version; a newer class file is refused when it is loaded.

  7. What does javac --release 17 change?

    Answer

    The class file version and the API the compiler checks against

    The compiler writes an older class file version and refuses API calls that Java 17 did not have.

  8. What does this program print? public class Q3 { public static void main(String[] args) { System.out.println("home: " + !System.getProperty("java.home").isEmpty()); } }

    Answer

    home: true

    java.home is always set, so isEmpty() is false and its negation is true.

  9. Why does the course build with javac -Xlint:all?

    Answer

    It turns on every compiler warning, so none is missed

    The flag enables all warning groups; it changes nothing about the class file or the run.

  10. Two JDKs are installed and java -version is older than javac -version. What is the likely cause?

    Answer

    The path finds java in one JDK and javac in another

    Each command is the first one on the path, so a mixed path picks one JDK for java and another for javac.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev