← All articles
Java · macOS

Setting JAVA_HOME on macOS

Some Java applications and build tools rely on the JAVA_HOME environment variable to locate the intended JDK. macOS provides /usr/libexec/java_home, which is preferable to guessing an installation path.

Confirm Java Is Available

which java

Then resolve the Java home directory:

/usr/libexec/java_home

For the environment used when this article was written, the result looked similar to:

/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

Set JAVA_HOME for the Current Shell Session

export JAVA_HOME=$(/usr/libexec/java_home)
echo $JAVA_HOME

If you need a specific installed version, use the -v option, for example:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)

Persist JAVA_HOME

The original article used ~/.bash_profile, which was common on macOS at the time. Add an export statement to the startup file used by your shell and then start a new terminal session.

Version context: Modern macOS commonly uses zsh rather than Bash as the default shell, so the appropriate configuration file may now be ~/.zshrc or another shell-specific startup file.

Takeaway

Resolve the JDK path dynamically with java_home where possible. It is less brittle than embedding a version-specific installation path directly into scripts or configuration files.

CONTINUE READING

Continue with closely related implementation guidance and architecture deep dives.

KEEP EXPLORING

More practical engineering notes

Browse the complete library or explore architecture-focused Deep Dives.

Browse articlesExplore Deep Dives