How to Set PATH and JAVA_HOME

Naveen AnamaneniUpdated · 5 min read

When you type java in a terminal, how does the computer know where Java is? It doesn't search your whole disk. It looks only in a short list of folders called PATH. Most "java is not recognized" problems come down to one thing: the JDK's folder isn't on that list. This lesson shows you how to check it and fix it.

What PATH Does

PATH is an environment variable: a named setting that every program started from the terminal can read. It holds a list of folders. When you type a command, the terminal checks these folders one by one, in order, and runs the first matching program it finds.

The terminal runs the first java it finds on PATH

Two things follow from this:

  1. If no folder on PATH contains java, you get "command not found" or "'java' is not recognized".
  2. If two folders contain java, the one listed first wins. That's how you end up running Java 17 when you installed Java 25.

What JAVA_HOME Does

JAVA_HOME holds the path of the JDK folder itself (the parent of bin). The terminal doesn't use it, but many tools do: Maven, Gradle, IDEs and application servers read JAVA_HOME to find which JDK to use.

VariablePoints toExample on WindowsUsed by
PATHA list of folders, including the JDK's bin...;C:\Program Files\Eclipse Adoptium\jdk-25.0.1.8-hotspot\binYour terminal
JAVA_HOMEOne JDK folderC:\Program Files\Eclipse Adoptium\jdk-25.0.1.8-hotspotMaven, Gradle, IDEs and other tools

Check Your Current Settings

Run these commands in a new terminal.

Terminal
where java
echo %JAVA_HOME%

where java (Windows) lists every java on PATH, in order. The first line is the one that runs. which java (macOS and Linux) shows the one that runs. If the echo line prints the variable's name back to you, or prints nothing, JAVA_HOME isn't set.

Set Them

Use the steps for your system. Replace the example JDK path with the real one on your computer. The InstallCheck program from the previous lesson prints it after Home:.

Steps
1. Press the Windows key, type "environment", and open
   "Edit the system environment variables".
2. Click "Environment Variables...".
3. Under "User variables", click "New...":
     Variable name:  JAVA_HOME
     Variable value: C:\Program Files\Eclipse Adoptium\jdk-25.0.1.8-hotspot
4. Select "Path" under "User variables", click "Edit...",
   then "New", and type:  %JAVA_HOME%\bin
5. Click "Move Up" until it is above any older Java entry.
6. Click OK on every window, then open a NEW terminal.

On macOS, /usr/libexec/java_home -v 25 finds the installed JDK 25 for you, so you don't have to type its long path. On Linux, ls /usr/lib/jvm lists the installed JDKs.

Read Them from Java

A Java program can read environment variables too. This is handy when something looks wrong and you want to see exactly what a program sees.

ShowJavaHome.java▶ Try it
public class ShowJavaHome {
    public static void main(String[] args) {
        String javaHome = System.getenv("JAVA_HOME");
        String running = System.getProperty("java.home");
 
        System.out.println("JAVA_HOME variable: " + javaHome);
        System.out.println("Java running now:   " + running);
    }
}
Output
JAVA_HOME variable: /Library/Java/JavaVirtualMachines/temurin-25.jdk/Contents/Home
Java running now:   /Library/Java/JavaVirtualMachines/temurin-25.jdk/Contents/Home

How it works

  1. Line 3 reads the environment variable JAVA_HOME with System.getenv. If it isn't set, you get null.
  2. Line 4 reads the system property java.home: the folder of the Java that is actually running this program.
  3. Lines 6 and 7 print both. If the two lines point to different JDKs, your tools and your terminal are using different versions of Java.

Try it yourself: run it on your computer. Do both lines point to the same JDK?

Common Mistakes

Adding the JDK folder instead of its bin folder to PATH. The terminal looks for java.exe or java directly inside each PATH folder. It doesn't look inside sub-folders.

Setting JAVA_HOME to the bin folder. JAVA_HOME is the JDK folder; tools add \bin themselves. Ending it with \bin makes Maven and Gradle look in bin\bin.

Testing in an old terminal. Changes to environment variables only reach terminals opened after the change.

Typing spaces around = on macOS or Linux. export JAVA_HOME = /usr/lib/jvm/... fails. Write JAVA_HOME=/usr/lib/jvm/... with no spaces.

Practice

  1. Easy: Run where java (Windows) or which java (macOS, Linux). Which folder is your java in? Is it inside JDK 25?
  2. Harder: Change ShowJavaHome to also print System.getenv("PATH"). Find the JDK's bin folder in that long line. Is it before or after other folders?

Quick check: PATH and JAVA_HOME

4 questions
1What does the terminal do with the PATH variable?
2Which folder should be on PATH?
3Who mainly uses JAVA_HOME?
4Two JDKs are on PATH: JDK 17 first, then JDK 25. Which one runs when you type java?

Key Takeaways

Was this tutorial helpful?

Lesson 11 of 482