Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On macOS, JAVA_HOME is not a fixed string you should hardcode blindly. From Lion through Mojave, the reliable way to discover the active JDK home is /usr/libexec/java_home, which resolves the correct Contents/Home path for the installed Java version you want.
Core Sections
The command Apple expects you to use
macOS provides /usr/libexec/java_home specifically for this purpose. If Java is installed correctly, it prints the selected JDK home directory.
To list all detected JDKs:
This is better than guessing paths because different JDK vendors and versions can coexist on the same machine.
What the actual path usually looks like
For Oracle, OpenJDK, Temurin, and similar modern installs, the physical JDK directories are commonly under:
A specific JDK home typically looks like this:
That final Contents/Home directory is what tools usually expect in JAVA_HOME, not the .jdk bundle root.
Older Apple-provided Java 6 installs on older macOS versions may appear under Apple-managed locations, but even there, java_home is the safer interface because it hides those layout differences.
Set JAVA_HOME in your shell
For a temporary shell session:
To pin a specific version, pass -v.
Then persist it in your shell startup file. For example, in ~/.zshrc:
Open a new shell or run source ~/.zshrc afterward.
Understand the version differences from Lion to Mojave
From Lion through older macOS releases, Apple’s own Java 6 was part of the platform story for many systems. By the time you reach newer releases such as High Sierra and Mojave, Java is no longer something to assume is present by default for development use. That is why hardcoded advice like “JAVA_HOME is always here” ages badly.
The cross-version rule that remains dependable is:
- install the JDK you actually want
- use
/usr/libexec/java_hometo resolve it - export that result into
JAVA_HOME
That approach is stable even if the on-disk directory name changes.
Verify what your tools are seeing
After setting the variable, confirm both the shell and Java agree.
If java -version shows one JDK but JAVA_HOME points somewhere else, your shell configuration and your effective runtime path are out of sync.
This often happens when developers install multiple JDKs and update only one piece of the environment.
Avoid hardcoding when possible
Build tools such as Maven, Gradle, and IDEs often honor JAVA_HOME, so setting it correctly once is useful. But scripting with /usr/libexec/java_home directly is often even safer than embedding a full vendor-specific path in shared scripts.
That keeps team machines and CI environments more portable.
Common Pitfalls
- Hardcoding a single JDK bundle path and assuming it will survive upgrades, vendor changes, or multiple installed JDKs.
- Setting
JAVA_HOMEto the.jdkbundle root instead of the actualContents/Homedirectory. - Updating
JAVA_HOMEin the current shell only and forgetting to persist it in the shell startup file. - Assuming older macOS versions and newer ones manage Java in exactly the same way.
- Checking only
java -versionand not verifying thatJAVA_HOMEand thePATHare aligned with the intended JDK.
Summary
- On macOS Lion through Mojave, the best way to find Java home is
/usr/libexec/java_home. - Modern JDKs usually live under
/Library/Java/JavaVirtualMachines/.../Contents/Home. - Set
JAVA_HOMEfromjava_homerather than hardcoding a guessed path. - Use
-Vto list installed JDKs and-vto choose a specific version. - Verify the shell, compiler, and runtime all point to the same JDK after configuration.
Related reading
- Where is Java Installed on Mac OS X?
- Where is Java's Array indexOf?
- Where is the application.properties file in a Spring Boot project?
- Where is the Java SDK folder in my computer? Ubuntu 12.04
- Where is the Keytool application?
- Where to add .ebextensions in a WAR?
- Where to get UTF-8 string literal in Java?
- Where to put Bean in Spring Boot?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.