Installing a JDK, and JAVA_HOME

Installing one, then proving which one you got — and the two variables that decide it.

7 min read🔧 Setting Up Java

Installing a JDK is easy. Knowing which one you just installed is the part that matters, because a machine can have four and the one that runs is decided by something you have not looked at.

Installing

macOS — Homebrew is the least trouble: brew install --cask temurin. Installers from the distribution's site work too.

Linux — your package manager (apt install temurin-21-jdk with Adoptium's repository, or your distribution's openjdk-21-jdk), or unpack a tarball anywhere and point at it.

Windows — the .msi from the distribution, which offers to set JAVA_HOME and PATH for you. Let it.

Any of them — a version manager (SDKMAN! on Unix, jabba, or your IDE's downloader) if you expect to switch between versions. Which you will, the first time you work on two projects.

The install is not the lesson. This is:

Prove what you got

Two commands, and they answer different questions:

bash
which java        # WHERE the java that runs comes from
java -version     # WHAT version that one is

Run both, every time you are surprised. java -version alone is the command people run and it cannot tell you that there are three other JDKs on the machine.

plaintext
$ which java
/usr/local/bin/java
 
$ java -version
openjdk version "21.0.12" 2026-07-21 LTS
OpenJDK Runtime Environment Temurin-21.0.12+8 (build 21.0.12+8-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.12+8 (build 21.0.12+8-LTS, mixed mode, sharing)

And check the compiler separately, because it is a different binary and can be a different version:

bash
javac -version

The two variables

PATH is a list of directories. When you type java, the shell walks it in order and runs the first match. That is what which is reporting.

JAVA_HOME points at the root of a JDK installation — not at the binary:

bash
JAVA_HOME=/usr/lib/jvm/temurin-21     # yes: the directory containing bin/, lib/
JAVA_HOME=/usr/lib/jvm/temurin-21/bin/java   # no

PATH is for the shell. JAVA_HOME is for other programs — Maven, Gradle, application servers, scripts — which need more than the launcher: they need javac, the libraries, the whole installation.

They can disagree, and that is the confusing afternoon above. Set both, to the same JDK:

bash
export JAVA_HOME=/usr/lib/jvm/temurin-21
export PATH="$JAVA_HOME/bin:$PATH"

Putting $JAVA_HOME/bin first is what makes that JDK win over anything installed elsewhere.

Those two lines belong in your shell's startup file (~/.zshrc, ~/.bashrc) — export in a terminal lasts only as long as that terminal.

What you actually installed

A JDK is a directory. Look inside bin/ and you find about thirty programs:

plaintext
jar jarsigner java javac javadoc javap jcmd jconsole jdb jdeprscan jdeps
jfr jhsdb jimage jinfo jlink jmap jmod jpackage jps jrunscript jshell
jstack jstat jstatd jwebserver keytool rmiregistry serialver

Most you will never type. Six are worth knowing the names of now, so that when a lesson later says "run jcmd", it is a tool you have rather than something to install:

ToolFor
javarun
javaccompile
jshelltry a line without a class
javapsee what javac produced
jcmdask a running JVM questions
jstackwhat every thread is doing right now

The last two are diagnostics, and the fact that they ship with the platform is unusual and genuinely useful. You do not install anything to debug a production Java process.

Under the hood: what JAVA_HOME and PATH actually do when you type java

PATH resolution is not the operating system's job — it is the shell's. When you type a bare command name, the shell (bash, zsh, cmd.exe, PowerShell) walks the directories listed in PATH, in order, looking for an executable with that name, and hands the first one it finds to the OS to actually start as a process. Nothing about this is Java-specific: it is the exact same lookup that finds git, node or python, and it is why which java (which performs that same search and reports the winner without running it) is the right tool for answering "which one will actually run" — java -version only ever tells you about whatever which would have found anyway.

JAVA_HOME, by contrast, is not consulted by the shell, the OS, or the java launcher itself at all. It is a convention: a variable that other programs — Maven's and Gradle's launch scripts, application servers, IDE build integrations — agree to read and then construct a path like $JAVA_HOME/bin/javac from, because they need more of the installation than just the one binary PATH would find. Nothing enforces that convention. A tool that doesn't bother to check JAVA_HOME simply falls back to whatever PATH gives it, which is exactly how the two variables end up disagreeing: PATH picked one JDK for your terminal, JAVA_HOME (set once, forgotten, pointing somewhere else) is what a build tool picks instead.

There is a second layer beneath both of these worth knowing about: the java you run is not a script, it is a small native launcher program. Its job is to parse the arguments meant for the JVM itself (-cp, -Xmx, and so on — these never reach your main), locate the actual JVM shared library relative to its own installation directory, load it, and hand control to it. PATH and JAVA_HOME only get you as far as finding that launcher; everything after that is the launcher finding the rest of its own installation.

Try it yourself

Establish the truth about your machine

  1. which java and java -version. Do they agree with what you think you installed?
  2. javac -version. Same major version?
  3. echo $JAVA_HOME. Is it set, and does it point at a directory containing bin/?
  4. ls $JAVA_HOME/bin | wc -l. How many tools did you get?
If something is wrong

JAVA_HOME empty: set it, in your shell startup file, and add $JAVA_HOME/bin to the front of PATH.

java and javac differ: which -a java and which -a javac list every match in PATH order, not just the winner. That shows you the competing installations.

which java points somewhere you did not install: on macOS /usr/bin/java is a stub that finds a JDK for you and is often not the one you want. Putting $JAVA_HOME/bin first in PATH overrides it.

Misconceptions

  • "Setting JAVA_HOME is what makes java runnable." It does nothing by itself. PATH (or an absolute path) is what actually lets the shell find the binary; JAVA_HOME only matters to the other tools that choose to read it.
  • "A script that works in my interactive shell will work the same way in cron, systemd or CI." Those run with a different, usually much smaller, PATH, and no shell startup file has ever been sourced for them — a frequent, exact-fit cause of "works on my machine" for this class of problem.

Sources

  • Bash Reference Manual, "Command Search and Execution" — how a shell resolves a bare command name against PATH.
  • Oracle/OpenJDK, "The java Command" tool reference — the launcher's own documented behavior.
  • Apache Maven documentation on JDK/JAVA_HOME detection in its launcher scripts — a concrete example of a tool that reads the variable by convention.
Progress is saved on this device and to your account when signed in.