JavaBasicsbeginner
Updated:

How to Install Java on Windows, macOS and Linux

6 min read

Install the JDK the right way on any operating system, configure JAVA_HOME and PATH, and confirm your setup so your first compile actually works.

TL;DR – Quick Answer

To install Java, download a JDK (Java Development Kit) such as the free OpenJDK or Oracle JDK for your operating system, run the installer, then set the JAVA_HOME and PATH environment variables so the terminal can find the java and javac commands. Confirm the install by running java -version, which should print the version you installed.

On This Page

Before you write a single line of Java, you need a working toolchain on your machine. Ninety percent of the "Java won't run" messages we get from new students trace back to one of two things: no JDK installed, or a JDK installed but not on the PATH. This guide fixes both, on all three major operating systems.

We will install the JDK, set the environment variables, and verify the result. If you are still unsure what the JDK even is, read JDK vs JRE vs JVM first, then come back here to actually put it on your computer.

Pick the right JDK before you download

The single most common beginner mistake is downloading the JRE instead of the JDK, or grabbing a random version. Get this decision right and the rest is mechanical.

  • You need the JDK, not the JRE. The JDK contains the javac compiler that turns your .java source into bytecode. The JRE only runs already-compiled programs. As a learner you compile constantly, so the JRE alone is useless to you.
  • Choose a Long-Term Support (LTS) version. In 2026 that means Java 17 or Java 21. LTS releases get years of updates and match what real employers run.
  • Free OpenJDK builds are fine. Adoptium Temurin, Amazon Corretto, and Azul Zulu are all free OpenJDK builds. Oracle's own JDK works too; just be aware its license has conditions for some production scenarios.

Pro tip: Stick to one JDK version while you are learning. Juggling Java 8, 17, and 21 on the same machine is a classic source of confusing errors. Add more versions later, once you understand how JAVA_HOME selects between them.

Install Java on Windows

Windows needs the most manual setup because it does not configure environment variables for you.

Step 1 — Download and run the installer. Get the .msi installer for an LTS JDK and run it. Accept the defaults; note the install folder, usually something like C:\Program Files\Eclipse Adoptium\jdk-21.

Step 2 — Set JAVA_HOME. Open Edit the system environment variablesEnvironment Variables. Under System variables, click New:

  • Variable name: JAVA_HOME
  • Variable value: the JDK folder, e.g. C:\Program Files\Eclipse Adoptium\jdk-21

Step 3 — Add Java to the PATH. In the same dialog, edit the Path variable and add a new entry:

%JAVA_HOME%\bin

Step 4 — Verify in a fresh terminal. Close every open Command Prompt or PowerShell window, then open a new one. Environment changes only apply to terminals opened after the change.

java -version
javac -version

Both should print a version number. If java works but javac does not, you installed a JRE, not a JDK — go back and install the JDK.

Common mistake: Editing Path and then testing in a terminal that was already open. The old terminal still holds the old environment. Always open a new window before you run java -version.

Install Java on macOS

macOS gives you two clean routes.

Option A — Homebrew (recommended). If you have the Homebrew package manager, one command handles the download, install, and most of the wiring:

brew install --cask temurin

Option B — the .pkg installer. Download the macOS .pkg for an LTS JDK and double-click it. It installs into /Library/Java/JavaVirtualMachines/.

Set JAVA_HOME using the built-in helper, which always points at your current JDK:

echo 'export JAVA_HOME=$(/usr/libexec/java_home)' >> ~/.zshrc
source ~/.zshrc
java -version

The /usr/libexec/java_home tool is a macOS convenience that resolves the active JDK path for you, so you never hard-code a version folder. If you use the older Bash shell, put the same line in ~/.bash_profile instead of ~/.zshrc.

Install Java on Linux

On most distributions the package manager is the fastest path.

Debian / Ubuntu:

sudo apt update
sudo apt install openjdk-21-jdk
java -version

Fedora / RHEL:

sudo dnf install java-21-openjdk-devel
java -version

The -devel (or -jdk) package is important — the plain java package on some distros is only the runtime. If javac is missing after install, you grabbed the runtime-only package.

To set JAVA_HOME persistently, find the real install path and export it in your shell profile:

readlink -f $(which java)   # prints the real java install path
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

Put the two export lines (with your actual path) into ~/.bashrc, run source ~/.bashrc to reload, then confirm with java -version. The readlink -f trick resolves the symbolic links most distributions use, so you set JAVA_HOME to the concrete JDK folder rather than a symlink that could later point elsewhere.

Verify the install with a real compile

java -version proves the runtime works, but it does not prove you can compile. Do a full round trip so you catch a missing compiler now instead of during your first Java program.

Create a file called Check.java:

public class Check {
    public static void main(String[] args) {
        System.out.println("JDK is installed and working");
    }
}

Then compile and run it from the folder that contains the file:

javac Check.java   # produces Check.class
java Check         # prints the message

If you see JDK is installed and working, your toolchain is complete: compiler, runtime, and PATH are all correct. If javac throws "command not found," your PATH points at a JRE or nothing at all — recheck the bin folder in JAVA_HOME.

For a slightly stronger check, compile a program that reports the runtime version from inside Java itself. This confirms the running JVM matches the version you think you installed, which is useful when several JDKs are present:

public class VersionCheck {
    public static void main(String[] args) {
        System.out.println("Running on Java " +
            System.getProperty("java.version"));
        System.out.println("JVM home: " +
            System.getProperty("java.home"));
    }
}

Compile with javac VersionCheck.java and run java VersionCheck. The java.version line should match your java -version output, and java.home shows the exact JDK the JVM launched from — the fastest way to catch a stray older install winning on the PATH.

Pro tip: The class you compile with javac Check.java becomes Check.class. When you run it, type java Check with no .class extension. Passing java Check.class is a frequent beginner error that produces a confusing "could not find or load main class" message.

Common setup problems and fixes

A few failures show up again and again in our beginner batches:

  • "java is not recognized" / "command not found": the bin folder is not on the PATH, or you did not reopen the terminal. Recheck JAVA_HOME and %JAVA_HOME%\bin.
  • java -version shows an old version you thought you removed: an older JDK is earlier on the PATH. Fix the PATH order or update JAVA_HOME to the version you want as default.
  • javac missing but java works: you installed a JRE or a runtime-only Linux package. Install the JDK / -devel package.
  • IDE uses a different version than the terminal: IntelliJ and Eclipse each store their own "Project SDK" setting. Point the IDE at the same JAVA_HOME so builds match.
  • Two JDKs fight on Windows: some installers add their own entry to the front of Path. If the wrong version wins, move %JAVA_HOME%\bin above the others in the Path list so it is found first.

The mental model that prevents most of these problems is simple. JAVA_HOME tells other tools — Maven, Gradle, your IDE — which JDK to use, while the PATH entry is what lets you type java and javac in any folder without the full path. When both point at the same LTS JDK, every tool on your machine agrees on the version, and confusing "works in the IDE but not the terminal" bugs disappear.

Understanding why these variables matter also helps you in interviews. Knowing the difference between the JDK, JRE, and JVM and how the JVM finds and loads classes is exactly the kind of fundamentals question freshers get asked in their first technical round.

What to do next

With Java installed and verified, you are ready to write and run real code. Head straight to Your First Java Program to compile Hello World and understand every line of it, then start on variables and data types to build from there.

If you would rather learn with a structured path and a mentor checking your setup and your code, that is how we run the first week of the Java Full Stack course — everyone gets the same environment working before we touch syntax, so nobody is stuck debugging a PATH on day three. You can also browse the full Java learning hub to see how installation fits into the wider roadmap.

Frequently Asked Questions

Do I need the JDK or the JRE to start learning Java?
You need the JDK. The JRE only runs compiled programs, but as a learner you also need to compile your own code with the javac command, and that compiler ships only inside the JDK. Modern JDK downloads include everything the JRE offered, so installing a single JDK is enough.
Which Java version should a beginner install in 2026?
Install a Long-Term Support release such as Java 17 or Java 21. LTS versions receive years of security updates and are what most companies run in production. Avoid the very newest non-LTS release for daily learning because tutorials and libraries lag behind it.
Is OpenJDK the same as Oracle JDK?
They are built from the same source code and behave identically for almost all learning and job work. OpenJDK builds from Adoptium, Amazon Corretto, or Azul are free for commercial use, while Oracle's own build has licensing conditions for some production use. For a beginner, any reputable OpenJDK build is a safe, free choice.
Why does my terminal say 'java is not recognized' after installing?
That error means the folder containing the java executable is not on your PATH. On Windows this usually means JAVA_HOME and the bin folder were not added to the system PATH, or you did not open a fresh terminal after editing them. Reopen the terminal so it reloads the environment variables and try again.
Do I need to install Java before installing an IDE like IntelliJ or Eclipse?
It is cleaner to install the JDK first so you control exactly which version you have. Some IDEs can download a JDK for you, but installing it yourself and setting JAVA_HOME means command-line tools, build tools like Maven, and every IDE all share the same known version.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — Explore the Program

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 15 July 2026 LinkedIn
Chat with us