JAVA_HOME is an environment variable that points to your Java installation directory. Many Java-based applications and build tools (Maven, Gradle, Tomcat) require this variable to be set correctly.
Why Set JAVA_HOME?
Required by build tools : Maven, Gradle, and Ant need it to find Java
Application servers : Tomcat, JBoss, and others use it
IDE compatibility : Ensures consistent Java version across tools
Script automation : Makes Java location consistent across systems
Finding Your Java Installation Linux 1 2 3 4 5 6 7 8 which javareadlink -f $(which java)update-alternatives --list java
Common locations:
/usr/lib/jvm/java-11-openjdk-amd64
/usr/lib/jvm/java-17-openjdk-amd64
/usr/java/jdk-17.0.1
macOS 1 2 3 4 5 6 7 8 /usr/libexec/java_home /usr/libexec/java_home -V /usr/libexec/java_home -v 17
Common locations:
/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
/Applications/Android Studio.app/Contents/jbr/Contents/Home
Windows 1 2 3 4 5 # From Command Prompt where java # From PowerShell (Get-Command java).Path
Common locations:
C:\Program Files\Java\jdk-17
C:\Program Files\OpenJDK\jdk-17.0.1
C:\Program Files\Eclipse Adoptium\jdk-17.0.1-hotspot
Setting JAVA_HOME Linux Temporary (Current Session Only) 1 2 export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64export PATH=$PATH :$JAVA_HOME /bin
Permanent (All Sessions) Add to ~/.bashrc or ~/.zshrc:
1 2 3 4 5 6 7 8 9 nano ~/.bashrc export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64export PATH=$PATH :$JAVA_HOME /binsource ~/.bashrc
System-wide (All Users) Create a file in /etc/profile.d/:
1 2 3 4 5 6 7 8 9 10 11 12 sudo nano /etc/profile.d/java.shexport JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64export PATH=$PATH :$JAVA_HOME /binsudo chmod +x /etc/profile.d/java.shsource /etc/profile.d/java.sh
macOS Using Shell Configuration Add to ~/.zshrc (default shell on modern macOS):
1 2 3 4 5 6 7 8 9 nano ~/.zshrc export JAVA_HOME=$(/usr/libexec/java_home)export PATH=$JAVA_HOME /bin:$PATH source ~/.zshrc
For Specific Java Version 1 2 3 4 5 6 7 8 export JAVA_HOME=$(/usr/libexec/java_home -v 17)export JAVA_HOME=$(/usr/libexec/java_home -v 11)export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
For Android Studio’s JDK 1 2 export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home" export PATH=$JAVA_HOME /bin:$PATH
Windows Using System Properties (GUI)
Open Start Menu , search for “Environment Variables”
Click Edit the system environment variables
Click Environment Variables button
Under System variables , click New
Set:
Variable name: JAVA_HOME
Variable value: C:\Program Files\Java\jdk-17 (your Java path)
Click OK
Find Path variable, click Edit
Click New and add: %JAVA_HOME%\bin
Click OK on all dialogs
Restart any open Command Prompts
Using Command Prompt (Admin) 1 2 setx JAVA_HOME "C:\Program Files\Java\jdk-17 " /M setx PATH "%PATH% ;%JAVA_HOME% \bin" /M
Note : /M sets system-wide. Remove it for user-only.
Using PowerShell (Admin) 1 2 3 [System.Environment ]::SetEnvironmentVariable("JAVA_HOME" , "C:\Program Files\Java\jdk-17" , "Machine" ) $path = [System.Environment ]::GetEnvironmentVariable("PATH" , "Machine" )[System.Environment ]::SetEnvironmentVariable("PATH" , "$path ;%JAVA_HOME%\bin" , "Machine" )
Verify Installation After setting JAVA_HOME, verify it’s working:
Linux/macOS 1 2 3 4 5 6 7 8 9 10 11 echo $JAVA_HOME java -version javac -version $JAVA_HOME /bin/java -version
Expected output:
1 2 3 openjdk version "17.0.1" 2021-10-19 OpenJDK Runtime Environment (build 17.0.1+12) OpenJDK 64-Bit Server VM (build 17.0.1+12, mixed mode)
Windows 1 2 3 4 5 6 7 8 # Check JAVA_HOME value echo %JAVA_HOME% # Verify Java is accessible java -version # Verify javac (compiler) is accessible javac -version
Common Issues “JAVA_HOME is not defined” Solution : Ensure you’ve reloaded your shell or restarted your terminal after setting the variable.
“command not found: java” after setting JAVA_HOME Solution : Make sure you also added $JAVA_HOME/bin to your PATH.
1 2 3 4 5 echo $PATH | grep "$JAVA_HOME " export PATH=$PATH :$JAVA_HOME /bin
Wrong Java version being used Solution : Check if multiple Java installations exist and PATH order.
1 2 3 4 5 6 7 8 which -a javaecho $PATH export PATH=$JAVA_HOME /bin:$PATH
Maven/Gradle still can’t find Java Solution : Some tools need explicit JAVA_HOME. Verify the path points to JDK, not JRE:
1 2 3 4 5 ls $JAVA_HOME ls $JAVA_HOME /bin/javac
Multiple Java Versions If you need to switch between Java versions:
Linux - Using update-alternatives 1 2 3 4 sudo update-alternatives --config java
macOS - Using jenv 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 brew install jenv echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrcecho 'eval "$(jenv init -)"' >> ~/.zshrcjenv add /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home jenv add /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home jenv global 17 cd your-projectjenv local 11
Windows - Manual switching Create batch files for each version:
java11.bat:
1 2 3 @echo off setx JAVA_HOME "C:\Program Files\Java\jdk-11 " /M echo Java 11 is now active
java17.bat:
1 2 3 @echo off setx JAVA_HOME "C:\Program Files\Java\jdk-17 " /M echo Java 17 is now active
Quick Reference Linux/macOS 1 2 3 4 5 6 7 8 9 10 11 export JAVA_HOME=/path/to/javaexport PATH=$JAVA_HOME /bin:$PATH export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64export PATH=$JAVA_HOME /bin:$PATH echo $JAVA_HOME java -version
Windows (Command Prompt) 1 2 3 4 5 6 7 # Set JAVA_HOME permanently (run as Admin) setx JAVA_HOME "C:\Program Files\Java\jdk-17 " /M setx PATH "%PATH% ;%JAVA_HOME% \bin" /M # Verify (restart CMD first) echo %JAVA_HOME% java -version
macOS with Android Studio JDK 1 2 3 4 5 6 export JAVA_HOME="/Applications/Android Studio.app/Contents/jbr/Contents/Home" export PATH=$JAVA_HOME /bin:$PATH source ~/.zshrc
Additional Environment Variables If you’re also developing Android apps, you might need:
1 2 3 4 5 6 7 export ANDROID_HOME=$HOME /Library/Android/sdkexport PATH=$PATH :$ANDROID_HOME /tools:$ANDROID_HOME /platform-toolsexport ANDROID_HOME=$HOME /Android/Sdkexport PATH=$PATH :$ANDROID_HOME /tools:$ANDROID_HOME /platform-tools
For Node.js version management with NVM:
1 2 3 4 export NVM_DIR="$HOME /.nvm" [ -s "$NVM_DIR /nvm.sh" ] && \. "$NVM_DIR /nvm.sh" [ -s "$NVM_DIR /bash_completion" ] && \. "$NVM_DIR /bash_completion"
Conclusion Setting JAVA_HOME correctly ensures your Java development tools work smoothly. Remember to:
Use the JDK directory, not JRE
Add $JAVA_HOME/bin to your PATH
Reload your shell after making changes
Verify with java -version and echo $JAVA_HOME
For projects requiring specific Java versions, consider using version managers like jenv (macOS/Linux) or SDKMAN! (all platforms).