macOS: JAVA_HOME environment Setup

Leo N
3 min readSep 13, 2021

1. What is Java Home

Many Java applications need to know the location of a $JAVA_HOME directory. The $JAVA_HOME on Mac OS X should be found using the /usr/libexec/java_home command-line tool on Mac OS X 10.5 or later. On older Mac OS X versions where the tool does not exist, use the fixed path "/Library/Java/Home". The /usr/libexec/java_home tool dynamically finds the top Java version specified in Java Preferences for the current user. This path allows access to the bin subdirectory where command-line tools such as java, javac, etc. exist as on other platforms. The tool /usr/libexec/java_home allows you to specify a particular CPU architecture and Java platform version when locating a $JAVA_HOME.

Another advantage of dynamically finding this path, as opposed to hardcoding the fixed endpoint, is that it is updated when a new version of Java is downloaded via Software Update or installed with a newer version of Mac OS X. For this reason, it is important that developers do not install files in the JDKs inside of /System, since the changes will be lost with subsequent updates by newer versions of Java.

To obtain the path to the currently executing $JAVA_HOME, use the java.home System property.

Some applications use Java Home:

2. Bash or Zsh?

  • For bash shell, we can put the environment variables at ~/.bash_profile or ~/.bashrc.
  • For zsh shell, we can put the environment variables at ~/.zshenv or ~/.zshrc.

3. /user/libexec/java_home

2.1 Open Terminal and run: /usr/libexec/java_home to get default JDK location.

If you do not see any SDK, please install by 2 ways:

2.2 Find all installed JDKs: /usr/libexec/java_home -V

--

--

Leo N

🎓 “A person who never made a mistake never tried anything new.” — Albert Einstein