JB Header

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.
  • 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.
Whichever you choose, make sure you download the JDK - not just a JRE - for your operating system and processor. Most Windows and Linux computers use x64. Macs with Apple silicon (M1 and later) use aarch64, also shown as ARM64. What are JAVA_HOME and PATH? Two environment variables matter for Java -
  • PATH: The list of folders your operating system searches when you type a command. The JDK's bin folder must be on the PATH, so that commands such as java and javac work from any folder.
  • JAVA_HOME: Points to the folder where the JDK is installed (the folder containing bin, not bin itself). You do not need it to run javac and java, 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.
Installing the JDK on Windows
  1. Download the Windows x64 installer (.msi) of the JDK from your chosen vendor - for example Temurin 25 from adoptium.net.
  2. 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 uses C:\Program Files\Java\jdk-25, adds Java to the PATH, but does not set JAVA_HOME.
  3. Open a new Command Prompt or PowerShell window. Windows only gives the new environment variables to windows opened after the installation.
If you installed from a .zip file, or your installer did not set JAVA_HOME, set the variables yourself -
  1. Press the Windows key, type environment variables and choose Edit environment variables for your account.
  2. Under User variables, click New. Enter JAVA_HOME as the name and the JDK folder, for example C:\Program Files\Java\jdk-25, as the value.
  3. Select the Path variable, click Edit, then New, and add %JAVA_HOME%\bin. Use Move Up to place it above any other Java entries.
  4. Click OK in each dialog, and open a new terminal window.
You can also set JAVA_HOME from PowerShell. This sets it for your user account -
[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk-25", "User")
Note - Avoid changing 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
  1. Download the macOS installer (.pkg) for your processor - aarch64 for Apple silicon Macs, x64 for older Intel Macs.
  2. Open the .pkg file and follow the steps. The JDK is installed under /Library/Java/JavaVirtualMachines/, and the java and javac commands work straight away in a new Terminal window.
If you use Homebrew, you can install Temurin with a single command instead -
brew install --cask temurin
macOS has a helper, /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)
Run 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
Note - Install the package ending in -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"
If several JDKs are installed, 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>.
Verifying the installation Open a new terminal window and check the versions of the java launcher and the javac compiler. Both must be present, and they should show the same version -
java -version
javac -version
 OUTPUT of the above commands
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
Next, check JAVA_HOME. On Windows Command Prompt use echo %JAVA_HOME%; in PowerShell use $env:JAVA_HOME; on macOS and Linux use echo $JAVA_HOME -  OUTPUT of echo %JAVA_HOME% on Windows
C:\Program Files\Java\jdk-24
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.
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"));
  }
}
Save it as CheckJava.java, then compile and run it -
javac CheckJava.java
java CheckJava
 OUTPUT of the above code
Java is installed and working!
Java version : 24.0.1
Java home    : C:\Program Files\Java\jdk-24
Explanation of the code
  • javac compiled CheckJava.java into CheckJava.class, which shows the compiler is installed.
  • java started 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.
Common problems and how to fix them
  • 'java' is not recognized as an internal or external command (Windows), or command not found: java (macOS, Linux): The JDK's bin folder 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 bin folder 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 java to list every java.exe found, in order; on macOS and Linux, use which -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 its bin subfolder.
Here is the output of 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 java
C:\Program Files\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files\Java\jdk-24\bin\java.exe
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 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.