☕ Java
Setting JAVA_HOME
JAVA_HOME is an environment variable that points to your JDK installation directory. Most Java build tools — Maven, Gradle, Ant — require it to be set correctly. Here's what it is, why it matters, and how to set it permanently on Windows, macOS, and Linux.
What Is JAVA_HOME and Why Does It Matter?
JAVA_HOME is an environment variable that tells your operating system — and any tools running on it — where the JDK is installed. It points to the root directory of your JDK installation, not the bin folder.
Tools like Maven, Gradle, Tomcat, Jenkins, and Android Studio all read JAVA_HOME to find the JDK they need. If it's not set, or points to the wrong location, these tools either fail to start or use the wrong Java version — often in confusing, hard-to-diagnose ways.
The java command working in your terminal doesn't mean JAVA_HOME is set. java works because the JDK's bin directory is on your PATH. JAVA_HOME is a separate variable that tools use independently.
Setting JAVA_HOME on Windows
On Windows, environment variables are set through System Properties. The changes persist across reboots and new terminal sessions.
Shell
# Option 1 — GUI (permanent):
# 1. Right-click "This PC" → Properties → Advanced system settings
# 2. Click "Environment Variables"
# 3. Under "System variables" → click "New"
# Variable name: JAVA_HOME
# Variable value: C:Program FilesEclipse Adoptiumjdk-21.0.1.12-hotspot
# 4. Find the "Path" variable → Edit → New → add: %JAVA_HOME%in
# 5. Click OK on all dialogs
# 6. Open a NEW Command Prompt (existing ones won't see the change)
# Option 2 — PowerShell (permanent, run as Administrator):
[System.Environment]::SetEnvironmentVariable(
"JAVA_HOME",
"C:Program FilesEclipse Adoptiumjdk-21.0.1.12-hotspot",
"Machine"
)
# Verify (in a new terminal):
echo %JAVA_HOME% # Command Prompt
$env:JAVA_HOME # PowerShellSetting JAVA_HOME on macOS
On macOS, macOS ships with a utility called java_home that returns the correct path for any installed JDK — the cleanest way to set JAVA_HOME dynamically.
Shell
# Find your JDK installation path:
/usr/libexec/java_home -v 21
# Output: /Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home
# Add to your shell profile (permanent):
# For zsh (default on macOS Catalina+):
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 21)' >> ~/.zshrc
source ~/.zshrc
# For bash:
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 21)' >> ~/.bash_profile
source ~/.bash_profile
# Verify:
echo $JAVA_HOME
# /Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home
# Switch between Java versions:
export JAVA_HOME=$(/usr/libexec/java_home -v 17) # switch to Java 17
export JAVA_HOME=$(/usr/libexec/java_home -v 21) # switch back to Java 21Setting JAVA_HOME on Linux
On Linux, JAVA_HOME is set in your shell's profile file. The exact file depends on your shell and whether you want the variable available system-wide or just for your user.
Shell
# Find your JDK installation path:
sudo update-alternatives --config java
# Note the path shown — strip /bin/java from the end
# Example: /usr/lib/jvm/java-21-openjdk-amd64
# Set for current user (permanent) — add to ~/.bashrc or ~/.zshrc:
echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
# Set system-wide (for all users) — add to /etc/environment:
sudo nano /etc/environment
# Add this line:
# JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"
# Or create a dedicated profile script:
sudo nano /etc/profile.d/java.sh
# Add:
# export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
# export PATH=$JAVA_HOME/bin:$PATH
# Verify:
echo $JAVA_HOME
java -versionCommon JAVA_HOME Mistakes
These are the four most common JAVA_HOME configuration errors:
Shell
# Mistake 1 — pointing to bin instead of JDK root:
JAVA_HOME=/usr/lib/jvm/java-21/bin # WRONG
JAVA_HOME=/usr/lib/jvm/java-21 # CORRECT
# Mistake 2 — pointing to JRE instead of JDK:
JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64/jre # WRONG (no compiler)
JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 # CORRECT
# Mistake 3 — not reloading the shell after setting:
# After editing .bashrc, run:
source ~/.bashrc # reload without closing terminal
# Mistake 4 — setting it only for current session (not permanent):
export JAVA_HOME=/path/to/jdk # only lasts until terminal closes
# Fix: add the export line to ~/.bashrc or ~/.zshrc insteadRelated Topics in Introduction
What is Java?
Java is a high-level, object-oriented programming language built on one killer idea: write your code once, and run it on any device — Windows, Mac, Linux, phone, smartwatch, you name it. No rewrites needed.
Features of Java
Java didn't become one of the world's most used languages by accident. From running on any device to handling millions of users simultaneously, here's what makes Java genuinely powerful — and why companies keep betting on it.
Uses of Java
Java powers everything from Android apps to banking systems, from Netflix's backend to NASA's mission control. Here's where Java is actually used in the real world — and why it keeps showing up in the most critical systems on the planet.
Java Editions (Java SE, EE, ME)
Java isn't one-size-fits-all. It comes in three distinct editions — each built for a different environment. Whether you're building a desktop app, a banking backend, or firmware for a SIM card, there's a Java edition designed exactly for that job.