Installing Java

In the last lesson you learned What is Java?. Now let’s get it onto your computer. You cannot run a line of Java until the right tool is installed. So this lesson is short and practical:

  • Install Java.
  • Check it really works.
  • Fix the small things that usually go wrong.

Think of it like a kitchen. Before you cook, the stove and pots must be in place. The Java install is that setup step. Get it right once, and every later lesson just works.

📦 What do you actually install?

People say “install Java”, but the thing you really install is the JDK (Java Development Kit). It is the full set of tools to write and run Java.

What the JDK gives you:

  • The compiler. Turns your code into bytecode, a halfway form the machine can run. Command: javac.
  • The runner. Takes that bytecode and actually runs it. Command: java.
  • The core libraries. Ready-made code, so you don’t build text handling or lists from scratch.

There’s also a smaller download, the JRE (Java Runtime Environment):

  • The JRE can only run Java, not compile it.
  • Grab it by mistake and java works but javac is missing.
  • So the rule is simple: to write Java, install the JDK, never just the runtime.

We go deep into JDK, JRE, and JVM next lesson. For now, hold on to that one rule.

Which version?

Pick a recent LTS version. LTS means Long-Term Support, a version that gets security fixes and updates for years instead of a few months. As of 2026, Java 21 is a great LTS choice. Any version 17 or higher is fine for everything in this course. Newer “feature” releases come out every six months, but those go out of support fast, so they are not ideal for steady learning.

🪟 Installing on Windows

The installer does almost all the work. Follow these steps in order:

  • Download the JDK. Use a trusted source like Adoptium (free, open source) or Oracle. Pick the Windows .msi for a recent LTS, 64-bit build.
  • Run the installer. Double-click it. Click Next. Keep the defaults.
  • Turn on “Add to PATH”. PATH is the list of folders your computer checks for a command. Adding Java lets you run java and javac from any folder.
  • Turn on “Set JAVA_HOME” too. Build tools and editors look for JAVA_HOME to find your JDK. Setting it now saves trouble later.
  • Finish and close.

One thing people forget: a terminal reads PATH only when it starts. So close every terminal you had open, then open a fresh one before you test.

Setting PATH and JAVA_HOME by hand on Windows

If the installer skipped this, or you used a plain .zip, set them yourself. Press the Windows key, type “environment variables”, open “Edit the system environment variables”, then click “Environment Variables”.

JAVA_HOME should point to the folder where the JDK lives:

C:\Program Files\Eclipse Adoptium\jdk-21.0.2.13-hotspot

Add a system variable named JAVA_HOME set to that folder. Then edit Path and add one more entry pointing inside it:

%JAVA_HOME%\bin

Why this works:

  • That \bin folder is where java.exe and javac.exe actually sit.
  • Using %JAVA_HOME% means an upgrade needs only one variable changed, and PATH follows.

Click OK, open a new terminal, and test.

🍎 Installing on Mac

Two easy paths. Pick whichever feels comfortable.

The simple way is the installer:

  • Download the macOS .pkg for a recent LTS from Adoptium.
  • For Apple Silicon (M1, M2, M3 and newer) choose aarch64. For older Intel Macs choose x64.
  • Open the file, click through. It places the JDK in the standard location for you.

Or use Homebrew, a tool that installs Mac software with one command. This installs a recent JDK:

Terminal window
brew install openjdk@21

Homebrew often prints a “next steps” message, because it sometimes does not link the JDK into your PATH. This command points your shell at the new java:

Terminal window
echo 'export PATH="/opt/homebrew/opt/openjdk@21/bin:$PATH"' >> ~/.zshrc

Reading that line:

  • export PATH=... sets the PATH for your shell.
  • The Homebrew bin folder goes first, so it is found before any old Java.
  • >> ~/.zshrc saves it into the startup file, so it loads every time.

Open a new terminal and check it worked.

🐧 Installing on Linux

On Linux you install through your package manager, the built-in tool that downloads and installs software. The command depends on your distribution.

For Ubuntu or Debian:

Terminal window
sudo apt update
sudo apt install openjdk-21-jdk

Reading these:

  • sudo runs as administrator, because installing system software needs permission.
  • apt update refreshes the package list, so you get current versions.
  • apt install openjdk-21-jdk installs the JDK. Pick -jre instead and you get only the runner, no compiler.

For Fedora, RHEL, or similar:

Terminal window
sudo dnf install java-21-openjdk-devel

The -devel ending matters here. It adds the development tools, which include javac. Without it you get just the runtime. Then move on to checking it worked.

✅ Check it worked

Never assume the install was fine. Always test it. Open your terminal (Command Prompt or PowerShell on Windows, Terminal on Mac and Linux) and type this:

Terminal window
java -version

This prints which version is installed. You’ll see something like this:

Output

openjdk version "21.0.2" 2024-01-16
OpenJDK Runtime Environment (build 21.0.2+13)
OpenJDK 64-Bit Server VM (build 21.0.2+13, mixed mode)

Your numbers will differ. That’s fine. A version number near the front means the runner is installed. Now check the compiler too, the half people miss:

Terminal window
javac -version

This should print a single line:

Output

javac 21.0.2

If both print a version, you’re fully set up. java proves the runner is there. javac proves the compiler is there. You need both, so test both.

You can also confirm JAVA_HOME points at the right place. On Mac or Linux:

Terminal window
echo $JAVA_HOME

On Windows PowerShell:

Terminal window
echo $env:JAVA_HOME

It should print the JDK folder path. If it prints nothing, JAVA_HOME is not set. Plain commands still run, but build tools want it, so set it using the steps above.

⚠️ Common Mistakes

What goes wrong during setup, and the fix:

  • java is not recognised” / “command not found”. PATH does not include Java. Add the JDK bin folder to PATH (on Windows, %JAVA_HOME%\bin), then open a fresh terminal.
  • Forgetting to reopen the terminal. The most common one. A terminal reads PATH only when it opens, so the new PATH loads only in a new window.
  • Installing only the JRE. java works but javac fails. Install the JDK instead (Fedora: the -devel package; Debian: the -jdk package).
  • An old Java taking priority. java -version shows an old number like 8 or 11 because an older Java is earlier in PATH. Put the new JDK’s bin first, then reopen the terminal.
  • Wrong build for your chip. On Mac, mixing Intel (x64) and Apple Silicon (aarch64) builds causes odd errors. Match the build to your hardware.

Old Java already installed?

Some computers ship with a very old Java. Run java -version first. If it shows something below 17, install a current LTS version and put it first on your PATH. That way every example in this course behaves the same for you as it does in the lesson.

🌟 Best Practices

Habits that keep your setup clean and save you from confusing errors:

  • Stick to one LTS version. Use Java 21 (or another LTS) for the whole course. Mixing versions is a common source of “why does this break”.
  • Always set JAVA_HOME. Build tools and editors look for it, so they just work.
  • Prefer a trusted source. Adoptium, Oracle, or your package manager. Random “download Java here” sites can bundle unwanted software.
  • Test both commands after every change. Run java -version and javac -version after any install or upgrade.
  • Reopen the terminal after any PATH change. Make it a reflex. Most “it still does not work” reports vanish in a fresh window.

🧩 What You’ve Learned

You are set up. Recap:

  • ✅ To write Java, you install the JDK (Java Development Kit), not just the JRE runtime.
  • ✅ The JDK gives you the compiler (javac), the runner (java), and the core libraries.
  • ✅ Pick a recent LTS version, like Java 21, so it stays supported for years.
  • ✅ You can install on Windows, Mac, or Linux using an installer or a package manager.
  • ✅ Set the PATH so commands are found, and set JAVA_HOME so tools can locate the JDK.
  • ✅ Always confirm it worked with java -version and javac -version, in a fresh terminal.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What do you install to write and run Java programs?

    Why: The JDK includes the compiler, the runner, and the core libraries you need to develop Java. The JRE alone can only run programs, not compile them.

  2. 2

    Which command confirms the Java compiler is installed?

    Why: java -version checks the runner; javac -version checks the compiler. You need both, so test both.

  3. 3

    Why pick an LTS version like Java 21?

    Why: LTS (Long-Term Support) versions get years of updates, which makes them a stable, safe choice for learning.

  4. 4

    You see 'java is not recognised' after installing on Windows. What is the most likely cause?

    Why: That error usually means Java's bin folder is missing from the PATH, or the terminal was opened before the PATH updated. Add it to PATH and open a new terminal.

🚀 What’s Next?

Java is installed and tested. Before we write a program, let’s clear up three names you will hear a lot: JDK, JRE, and JVM. Knowing the difference makes everything else easier.

JDK vs JRE vs JVM in Java

Share & Connect