How to Set PATH and JAVA_HOME
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.
Two things follow from this:
- If no folder on PATH contains
java, you get "command not found" or "'java' is not recognized". - 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.
| Variable | Points to | Example on Windows | Used by |
|---|---|---|---|
PATH | A list of folders, including the JDK's bin | ...;C:\Program Files\Eclipse Adoptium\jdk-25.0.1.8-hotspot\bin | Your terminal |
JAVA_HOME | One JDK folder | C:\Program Files\Eclipse Adoptium\jdk-25.0.1.8-hotspot | Maven, Gradle, IDEs and other tools |
Check Your Current Settings
Run these commands in a new terminal.
where java
echo %JAVA_HOME%which java
echo $JAVA_HOMEwhere 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:.
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.Add these two lines to the end of the file ~/.zshrc
(open it with: nano ~/.zshrc ):
export JAVA_HOME=$(/usr/libexec/java_home -v 25)
export PATH="$JAVA_HOME/bin:$PATH"
Save, then run: source ~/.zshrcAdd these two lines to the end of ~/.bashrc
(use the folder where your JDK really is):
export JAVA_HOME=/usr/lib/jvm/java-25-openjdk-amd64
export PATH="$JAVA_HOME/bin:$PATH"
Save, then run: source ~/.bashrcOn 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.
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);
}
}JAVA_HOME variable: /Library/Java/JavaVirtualMachines/temurin-25.jdk/Contents/Home
Java running now: /Library/Java/JavaVirtualMachines/temurin-25.jdk/Contents/HomeHow it works
- Line 3 reads the environment variable
JAVA_HOMEwithSystem.getenv. If it isn't set, you getnull. - Line 4 reads the system property
java.home: the folder of the Java that is actually running this program. - 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
- Easy: Run
where java(Windows) orwhich java(macOS, Linux). Which folder is yourjavain? Is it inside JDK 25? - Harder: Change
ShowJavaHometo also printSystem.getenv("PATH"). Find the JDK'sbinfolder in that long line. Is it before or after other folders?
Key Takeaways
Was this tutorial helpful?