Explain Codes LogoExplain Codes Logo

Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?

java
java-home
jdk-versions
homebrew
Anton ShumikhinbyAnton Shumikhin·Aug 12, 2024
TLDR

Query your JAVA_HOME location on macOS swiftly, like so:

echo $(/usr/libexec/java_home)

Inscribe this path directly into your shell environment:

echo 'export JAVA_HOME=$(/usr/libexec/java_home)' >> ~/.bash_profile

Recharge your profile without restarting:

source ~/.bash_profile

Execute java -version to reflect your new JAVA_HOME settings.

Juggling with Java versions

On occasion, specific Java versions will call. For such moments, add -v with /usr/libexec/java_home.

echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)' >> ~/.bash_profile

Hit the refresh button on your profile:

source ~/.bash_profile

Selecting suitable JDK

Interested in modifying JAVA_HOME or creating aliases for JDK versions? The /Library/Java/JavaVirtualMachines/ directory is your oracle. Directories (like jdk1.8.0_231.jdk) within hold each JDK version.

Brewing Java with Homebrew

For the Homebrew enthusiasts, installing Java is as simple as:

brew install openjdk

A symbolic link to /usr/local/opt/openjdk provides a constant path between multiple scripts and tools depending on a stable JAVA_HOME.

Visualization

Take a visual journey down the macOS landscape to find your JAVA_HOME as illustrated below:

🏞️ macOS System 🏞️ / \ 🗂️ /Library/Java/JavaVirtualMachines 🗂️ / \ 📂 Your JDK Folder 📂 / | \ 🧭 'bin' Folder - 'Home' Directory 🏠 <- JAVA_HOME

Embark on this quest:

Point A: /Library/Java/JavaVirtualMachines Point B: The 'Home' Directory in your JDK folder 🎯

Find your JAVA_HOME treasure here!

Multitasking with multiple JDKs

Juggling multiple Java environments may seem like a circus act, but jEnv is your safety net. This tool allows seamless version transitions without altering environment variables.

For Hadoop users, set JAVA_HOME in hadoop-env.sh. See your environment configuration for details.

Special JDK cases

Some tasks require special attention. For legacy software or specific development needs, you may have to:

  • Install legacy Java versions for old macOS.
  • Create multiple JAVA_HOME exports in .bash_profile for spur-of-the-moment Java version switches.
  • Configure Maven with Homebrew for a smoother ride when using Java and Hadoop.