How to Install the JDK and Set JAVA_HOME and PATH
Introduction
Before you can write and run Java programs, you need a JDK (Java Development Kit) installed on your computer. This tutorial shows you how to install the JDK and set the JAVA_HOME and PATH environment variables on Windows, macOS and Linux. We will start by choosing which JDK version and vendor to install. We will then go through the installation steps for each operating system. Lastly, we will verify the installation with a few commands and a small Java program, and look at the most common installation problems and their fixes.
(Note - If you are not sure what the JDK is, or how it differs from the JRE and the JVM, read JDK vs JRE vs JVMClick to read tutorial: JDK vs JRE vs JVM - Differences Explained with Examples first.) Which JDK should you install? There are two choices to make - the version and the vendor.
Installing the JDK on Windows
Note - Avoid changing
macOS has a helper,
Run
Note - Install the package ending in
Packages install the JDK under
If several JDKs are installed,
Verifying the installation
Open a new terminal window and check the versions of the
OUTPUT of the above commands
Next, check JAVA_HOME. On Windows Command Prompt use
Finally, let us confirm that Java can compile and run a program. The program below prints the version of Java that runs it, and the folder that Java is installed in.
Save it as
OUTPUT of the above code
Explanation of the code
Summary
In this tutorial we first chose a JDK - an LTS release such as Java 25 or 21, from a vendor such as Eclipse Temurin. We then understood what the PATH and JAVA_HOME environment variables do. Next we installed the JDK and set these variables on Windows, macOS and Linux. We then verified the installation with
(Note - If you are not sure what the JDK is, or how it differs from the JRE and the JVM, read JDK vs JRE vs JVMClick to read tutorial: JDK vs JRE vs JVM - Differences Explained with Examples first.) Which JDK should you install? There are two choices to make - the version and the vendor.
- Version: Choose a Long-Term Support (LTS) release. Java 25 (September 2025) is the latest LTS release, and Java 21 is the previous one. Both are excellent choices for learning, and everything in this Java Fundamentals series works on either of them.
- Vendor: All the popular JDKs are built from the same open-source OpenJDK code, so they behave the same. Eclipse Temurin (from adoptium.net) is free, widely used and a good default. Other free choices are Oracle JDK and Oracle OpenJDK, Amazon Corretto, Microsoft Build of OpenJDK and Azul Zulu.
- PATH: The list of folders your operating system searches when you type a command. The JDK's
binfolder must be on the PATH, so that commands such asjavaandjavacwork from any folder. - JAVA_HOME: Points to the folder where the JDK is installed (the folder containing
bin, notbinitself). You do not need it to runjavacandjava, but build tools such as Maven and Gradle, application servers and many IDEs use it to find the JDK. It is good practice to set it.
- Download the Windows x64 installer (
.msi) of the JDK from your chosen vendor - for example Temurin 25 from adoptium.net. - Run the installer. In the Temurin installer's Custom Setup screen, make sure both Add to PATH and Set JAVA_HOME variable are selected - the second one is not selected by default. The JDK is installed to a folder such as
C:\Program Files\Eclipse Adoptium\jdk-25.... Oracle's installer usesC:\Program Files\Java\jdk-25, adds Java to the PATH, but does not set JAVA_HOME. - Open a new Command Prompt or PowerShell window. Windows only gives the new environment variables to windows opened after the installation.
.zip file, or your installer did not set JAVA_HOME, set the variables yourself -- Press the Windows key, type environment variables and choose Edit environment variables for your account.
- Under User variables, click New. Enter
JAVA_HOMEas the name and the JDK folder, for exampleC:\Program Files\Java\jdk-25, as the value. - Select the
Pathvariable, click Edit, then New, and add%JAVA_HOME%\bin. Use Move Up to place it above any other Java entries. - Click OK in each dialog, and open a new terminal window.
[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk-25", "User")
Path with the old setx command. It cuts values longer than 1024 characters, which can silently damage your PATH. Use the Environment Variables dialog for PATH instead.
Installing the JDK on macOS
- Download the macOS installer (
.pkg) for your processor - aarch64 for Apple silicon Macs, x64 for older Intel Macs. - Open the
.pkgfile and follow the steps. The JDK is installed under/Library/Java/JavaVirtualMachines/, and thejavaandjavaccommands work straight away in a new Terminal window.
brew install --cask temurin
/usr/libexec/java_home, which finds installed JDKs. Use it to set JAVA_HOME by adding this line to the ~/.zshrc file in your home folder (zsh is the default shell on macOS) -
export JAVA_HOME=$(/usr/libexec/java_home -v 25)
source ~/.zshrc, or open a new Terminal window, for the change to take effect. /usr/libexec/java_home -V lists all the JDKs installed on your Mac.
Installing the JDK on Linux
Most Linux distributions provide OpenJDK packages. On Ubuntu and Debian, install a JDK with apt, and on Fedora, Red Hat and similar distributions use dnf. The version numbers available depend on your distribution's release; for example -
sudo apt update sudo apt install openjdk-21-jdk sudo dnf install java-21-openjdk-devel
-jdk (Debian, Ubuntu) or -devel (Fedora, Red Hat). The packages without it contain only the runtime, which has no javac.Packages install the JDK under
/usr/lib/jvm/ and put java on the PATH. To set JAVA_HOME, add a line like this, with your JDK's folder name, to ~/.bashrc -
export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 export PATH="$JAVA_HOME/bin:$PATH"
sudo update-alternatives --config java (Debian and Ubuntu) lets you choose which one the java command uses. Another popular option on Linux and macOS is SDKMAN! (sdkman.io), a tool which installs and switches between many JDK versions and vendors with commands such as sdk list java and sdk install java <version>.
java launcher and the javac compiler. Both must be present, and they should show the same version -
java -version javac -version
java version "24.0.1" 2025-04-15 Java(TM) SE Runtime Environment (build 24.0.1+9-30) Java HotSpot(TM) 64-Bit Server VM (build 24.0.1+9-30, mixed mode, sharing) javac 24.0.1
echo %JAVA_HOME%; in PowerShell use $env:JAVA_HOME; on macOS and Linux use echo $JAVA_HOME -
OUTPUT of echo %JAVA_HOME% on WindowsC:\Program Files\Java\jdk-24
A program to check your Java installation
//CheckJava.java
public class CheckJava {
public static void main(String[] args) {
System.out.println("Java is installed and working!");
System.out.println("Java version : " + System.getProperty("java.version"));
System.out.println("Java home : " + System.getProperty("java.home"));
}
}
CheckJava.java, then compile and run it -
javac CheckJava.java java CheckJava
Java is installed and working! Java version : 24.0.1 Java home : C:\Program Files\Java\jdk-24
javaccompiledCheckJava.javaintoCheckJava.class, which shows the compiler is installed.javastarted a JVM and ran the program, which shows the runtime works.System.getProperty("java.home")returns the folder of the Java installation which is running the program - it should match your JAVA_HOME.
- 'java' is not recognized as an internal or external command (Windows), or command not found: java (macOS, Linux): The JDK's
binfolder is not on the PATH. Add it as shown above, and remember to open a new terminal window. - java works but javac is not found: You have only a runtime installed, or an older JRE comes first in the PATH. Install a full JDK, and make sure its
binfolder is the first Java entry in the PATH. - java and javac show different versions: Two Java installations are on the PATH. On Windows, run
where javato list everyjava.exefound, in order; on macOS and Linux, usewhich -a java. Remove or reorder the entries so that the JDK you want comes first. - JAVA_HOME points to the wrong folder: JAVA_HOME must point to the JDK folder itself, such as
C:\Program Files\Java\jdk-25- not to itsbinsubfolder.
where java on a Windows machine with Oracle's JDK installed. The first entry is a small launcher folder which Oracle's installer adds to the PATH, and which points to the JDK in the second entry -
OUTPUT of where javaC:\Program Files\Common Files\Oracle\Java\javapath\java.exe C:\Program Files\Java\jdk-24\bin\java.exe
java -version, javac -version and a small Java program. Lastly, we went through the most common installation problems and their fixes. With the JDK installed, you are ready to choose an IDEClick to read tutorial: Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS Code and write your first Java programClick to read tutorial: Java Hello World Program Explained Line by Line.Java Fundamentals Tutorials
JDK vs JRE vs JVM - Differences Explained with ExamplesClick to read tutorial: JDK vs JRE vs JVM - Differences Explained with Examples Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS CodeClick to read tutorial: Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS Code What is Java? History, Features, Editions and UsesClick to read tutorial: What is Java? History, Features, Editions and Uses Java Hello World Program Explained Line by LineClick to read tutorial: Java Hello World Program Explained Line by Line Compiling and Running Java Programs with javac and javaClick to read tutorial: Compiling and Running Java Programs with javac and java How Java Code Runs: Bytecode, Class Loader and JITClick to read tutorial: How Java Code Runs: Bytecode, Class Loader and JIT JShell Tutorial: Java's Interactive REPL with ExamplesClick to read tutorial: JShell Tutorial: Java's Interactive REPL with Examples
JDK vs JRE vs JVM - Differences Explained with ExamplesClick to read tutorial: JDK vs JRE vs JVM - Differences Explained with Examples Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS CodeClick to read tutorial: Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS Code What is Java? History, Features, Editions and UsesClick to read tutorial: What is Java? History, Features, Editions and Uses Java Hello World Program Explained Line by LineClick to read tutorial: Java Hello World Program Explained Line by Line Compiling and Running Java Programs with javac and javaClick to read tutorial: Compiling and Running Java Programs with javac and java How Java Code Runs: Bytecode, Class Loader and JITClick to read tutorial: How Java Code Runs: Bytecode, Class Loader and JIT JShell Tutorial: Java's Interactive REPL with ExamplesClick to read tutorial: JShell Tutorial: Java's Interactive REPL with Examples