RizTech Academy logo
RizTech Academy
Getting StartedLesson 3 of 420 min

Installing Java 21 and IntelliJ IDEA

This is where people lose an afternoon, and almost always to the same two things: downloading the wrong JDK, and JAVA_HOME. Both are avoidable if you know they are coming.

You need two pieces of software: a JDK, and an IDE. Nothing else.

Which JDK to download

Search for "download Java" and the first result is Oracle's site, which asks you to create an account and shows a licence page. You do not need either.

Java is developed as OpenJDK, an open-source project. Several organisations compile that same source and publish builds. They are, for our purposes, interchangeable:

Build Published by Notes
Eclipse Temurin Eclipse Adoptium Use this. Free for anything, no account, installers for every platform.
Amazon Corretto Amazon Fine. Common on AWS.
Azul Zulu Azul Fine. Offers builds for very old platforms.
Microsoft Build of OpenJDK Microsoft Fine. Ships inside some Azure tooling.
Oracle JDK Oracle Also free to download, under a licence that has changed several times. Avoid the question.
Red Hat build Red Hat Usual choice on RHEL.

Install Eclipse Temurin 21 (LTS) from adoptium.net. It is free under any circumstances, needs no login, and is the build most CI systems use.

If your machine already has a JDK, check its version before installing another — java --version. If it says 21, you are done with this section.

Windows

Download the .msi installer for Temurin 21, JDK, x64. Run it, and on the custom-setup screen turn on these two, which are off by default:

  • Set JAVA_HOME variable
  • Add to PATH

That is the whole trick on Windows. Skipping them is the single most common reason a Windows install "does not work".

Then — and this matters — open a new terminal. A terminal that was already open has an old copy of the environment and will keep telling you Java is not installed no matter how many times you reinstall it.

java --version
javac --version

macOS

With Homebrew, which is worth having anyway:

brew install --cask temurin@21

Without Homebrew, download the .pkg for your chip — aarch64 for Apple silicon (M1 and later), x64 for Intel Macs — and run it.

macOS keeps JDKs in /Library/Java/JavaVirtualMachines/ and has a helper that lists them:

/usr/libexec/java_home -V

Set JAVA_HOME in your shell profile. On any recent macOS the shell is zsh, so that is ~/.zshrc:

export JAVA_HOME=$(/usr/libexec/java_home -v 21)
export PATH="$JAVA_HOME/bin:$PATH"

Then source ~/.zshrc, or open a new terminal tab.

Writing it that way rather than hardcoding a path means an update to 21.0.9 does not break your setup.

Linux

Debian or Ubuntu:

sudo apt update
sudo apt install openjdk-21-jdk

Fedora:

sudo dnf install java-21-openjdk-devel

Note -jdk and -devel. Install openjdk-21-jre instead and you get a runtime with no javac, which produces a confusing morning.

If your distribution's repositories are stuck on an older Java, use SDKMAN instead — it is the nicest way to manage JDKs on Linux and macOS:

curl -s "https://get.sdkman.io" | bash
sdk install java 21.0.5-tem
sdk use java 21.0.5-tem

SDKMAN sets JAVA_HOME for you and lets you switch versions per terminal, which becomes valuable the first time a project needs Java 17 and another needs 21.

Verify properly

Run both commands. Every time.

java --version
javac --version

Expected output, with the vendor and patch number varying:

openjdk 21.0.5 2024-10-15 LTS
OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.5+11 (build 21.0.5+11-LTS, mixed mode)

javac 21.0.5

If java works and javac says "command not found", you have a runtime without a compiler on your PATH — usually an old JRE, or the -jre package on Linux. Install the JDK.

If the two report different versions, you have two Javas and the PATH is finding pieces of both. Fix it before writing a line of code; this one produces errors that make no sense.

JAVA_HOME, and why PATH is not enough

PATH tells your shell where to find the java command. JAVA_HOME tells other programs where the whole JDK lives — and Maven, Gradle, Tomcat, and a good deal of CI tooling read only JAVA_HOME.

# macOS / Linux
echo $JAVA_HOME

# Windows PowerShell
echo $env:JAVA_HOME

An empty result here with a working java --version is the classic half-configured install. It works until module 10, when Maven appears and announces that it cannot find a JDK on a machine where Java plainly runs.

The trap worth naming. On a machine with two JDKs it is entirely possible for java --version to report 21 while mvn --version reports 17, because the two read different variables. When a build behaves differently from the program you just ran by hand, check both.

The IDE

Install IntelliJ IDEA Community Edition, free, from JetBrains.

Community Edition is enough for this entire course and for a great deal of professional Java. The paid Ultimate edition adds framework support — Spring, databases, HTTP clients — which matters later and not yet. If you are a student, JetBrains grants Ultimate free against a college email address; that is the legitimate route, and there is no reason to look for any other.

The honest alternative. If your laptop has 8 GB of RAM and is already struggling, IntelliJ plus a JVM plus a browser is a heavy load. VS Code with the Extension Pack for Java is noticeably lighter and perfectly usable for a foundation course. You lose refactoring quality and gain about a gigabyte of RAM. Java teams overwhelmingly use IntelliJ, so learn its shortcuts if you can run it.

First project

New Project, then:

  • Language: Java
  • Build system: IntelliJ — Maven comes in module 10, and adding it now only puts two unexplained files in front of you
  • JDK: the 21 you just installed. If the list is empty, Add JDK and point it at the directory JAVA_HOME holds.
  • Untick "Add sample code" so your first file is genuinely yours.

Call it java-foundation.

Two settings to change immediately

In Settings → Editor → General → Auto Import, turn on Add unambiguous imports on the fly and Optimise imports on the fly. Java requires an import line for almost every type you use, and typing them by hand is pure tedium the IDE should absorb.

Shortcuts worth learning in week one

Learn these five before the rest. They are the ones you use a hundred times a day.

Action macOS Windows / Linux
Run the current file Ctrl+R Shift+F10
Show me the fix Alt+Enter Alt+Enter
Search everywhere Shift Shift Shift Shift
Reformat code Cmd+Alt+L Ctrl+Alt+L
Go to declaration Cmd+B Ctrl+B
Rename everywhere Shift+F6 Shift+F6
Generate constructor, getters Cmd+N Alt+Insert
Find any action by name Cmd+Shift+A Ctrl+Shift+A

Alt+Enter is the important one. Any time IntelliJ underlines something in red, put the cursor on it and press Alt+Enter — it will offer the import, the cast, the missing method, or the variable declaration. In Java, where the compiler is strict, this shortcut does an enormous amount of work.

Check your work

Which JDK, and why that one? Eclipse Temurin 21, from adoptium.net. It is an OpenJDK build, free under all circumstances, and needs no account. Oracle's own download is not wrong, it just invites a licensing question you do not need to have.

Why run both java --version and javac --version? Because a runtime without a compiler is a real and common state — an old JRE on the PATH, or the -jre package on Linux. If only java works, you cannot compile anything.

What is JAVA_HOME for, given that PATH already works? PATH serves your shell. JAVA_HOME is what other tools read — Maven, Gradle, Tomcat, CI runners. A machine can run java perfectly and still have Maven claim there is no JDK.

You installed on Windows and the terminal still says Java is not recognised. Open a new terminal. The one you have was started before the installer changed the environment. If a new one also fails, the installer's "Add to PATH" option was left off — rerun it.

java --version says 21 and mvn --version says 17. What is going on? Two JDKs installed, and the two tools are reading different settings — PATH points at 21, JAVA_HOME at 17. Point both at the same JDK.

Practice

  1. Install Temurin 21 and verify with both commands. Do not continue until java --version and javac --version both report 21.

  2. Print your JAVA_HOME. If it is empty, set it now rather than in module 10 when Maven complains.

  3. Break your PATH on purpose, then fix it. In a terminal, run export PATH=/usr/bin:/bin on macOS or Linux — or on Windows $env:Path = "C:\Windows\System32" in PowerShell. Run java --version and watch it fail. Close the terminal, open a new one, confirm it works again. This teaches you that these settings are per-terminal and that closing one undoes a mess, which is worth knowing before you panic at something real.

  4. Install IntelliJ IDEA Community and create the java-foundation project. Leave the build system as IntelliJ. Confirm the Project SDK shows 21 in File → Project Structure.

  5. Find jshell from inside IntelliJ. Press Cmd+Shift+A or Ctrl+Shift+A, type "JShell", and open the console. Evaluate "Pune".toUpperCase(). Locating things by action name rather than by hunting menus is the habit that makes the IDE fast.

Next: your first program, and a proper answer to what every word in public static void main(String[] args) is doing.

Stuck on this lesson?

Being stuck is part of it — but being stuck alone for three days is not. Our internship programme pairs this curriculum with code review and one-to-one help from working developers, and it is free.

About the internship