Lesson · Java Roadmap & Environment
Installing a JDK — and keeping it working
11 min readInstalling Java
JDK vs JRE vs JVM, what JAVA_HOME is really for, how to verify an install in three commands, and why you'll want jshell open for the rest of this course.
JDK, JRE, JVM — the three acronyms
One paragraph of history prevents years of confusion. The three names describe nested subsets, and only one of them is a thing you install:
- JVM — the virtual machine that executes bytecode. It is part of every runtime, and you never install it separately.
- JRE — JVM + the core class libraries, enough to *run* Java programs but not compile them. Since Java 9 the JRE stopped shipping as its own download; you build one with jlink if you need it.
- JDK — JRE + the development tools: javac (compiler), jshell, javap, jar, jlink, jcmd. This is what you install.
Install once, verify in three commands
Install a current LTS JDK for your OS — a package manager (winget, brew, apt), an installer from a distribution like Temurin or Corretto, or SDKMAN! which also manages multiple versions. The install itself is uninteresting; the environment *around* it is where people get stuck.
Two environment variables matter: JAVA_HOME should point at the JDK's root directory, and PATH should include $JAVA_HOME/bin so the commands resolve. JAVA_HOME is not read by the java launcher at all — it is read by *build tools* (Maven, Gradle, IDEs) to find 'the JDK to use'. That is why a wrong JAVA_HOME breaks builds while java still works in your terminal.
Verify like this, and read the output — not just the exit code:
- java -version — which *runtime* your shell resolves, and its exact build.
- javac -version — which *compiler*, and that a JDK (not a bare JRE) is on the path.
- echo $JAVA_HOME (Windows: echo %JAVA_HOME%) — that it names the JDK root, and
where java(Windows) /which java(Unix) to reveal duplicates.
jshell — the REPL you'll live in
jshell ships with every JDK: type Java, get results, no file, no build, no main. This course leans on it constantly — when an explanation says 'try this', jshell is how you try it in five seconds instead of five minutes.
It is a full environment: declare variables, define methods and classes, import anything, and read back what you created. When it evaluates an expression it stores the result in $1, $2, … so you can keep poking at it. Leave with /exit.
- var name = "AlgoLab" — typed at the prompt, exactly one line.
- /vars /methods /types — list what you have defined so far; nothing is hidden.
- /help — the full command list, including /save and /open for turning a session into a file.
Multiple JDKs, and the IDE question
You will eventually have more than one JDK — an old project on 11, a new one on 21. SDKMAN! and jenv manage the switching: list candidates, install, set a default, and override per shell or per project directory. The mechanism underneath is always the same two variables, so once you understand JAVA_HOME/PATH you can fix any environment by hand.
IDEs bundle their own JDK, and that is usually a *different* one from your terminal's. Know where each tool's JDK is configured — IntelliJ's Project SDK / Gradle JVM settings, VS Code's java.jdt.ls.java.home — because 'it compiles in the IDE but not in the terminal' is always this mismatch. For this course, either IDE is fine; jshell is the great equaliser.
JAVA_HOME points at the JDK root — not at bin
Set JAVA_HOME to .../jdk-21, not .../jdk-21/bin. Build tools append /bin themselves; if you point at bin, they look for bin/bin/java and fail with confusing 'no JDK found' errors while your shell happily runs java.
Interview angles
JVM vs JRE vs JDK — one sentence each.+
JVM: the virtual machine that executes bytecode. JRE: JVM plus core libraries, enough to run but not compile. JDK: JRE plus development tools — compiler, jshell, javap — the thing you install as a developer.
What actually reads JAVA_HOME?+
Build tools and launchers (Maven, Gradle, IDE tooling), not the java command itself. That's why a broken JAVA_HOME shows up as build failures while java -version still works in the same shell.
How do you try a Java API without creating a project?+
jshell. It's a REPL in every JDK: variables, methods, imports, one line at a time, storing results in $1, $2 … — ideal for checking behaviour like string methods or collection semantics.
Why can the IDE compile code the terminal cannot?+
They are using different JDKs. The IDE bundled one and points its build at it; your shell resolves another via PATH/JAVA_HOME. Check each tool's configured JDK before doubting the code.