Set $JAVA_HOME environment variable on macOS

Steps to set the $JAVA_HOME environment variable on macOS.

  1. Find out your macOS version.
  2. Find out which shell you are using, bash or zsh?
  3. For zsh shell, export $JAVA_HOME at ~/.zshenv or ~/.zshrc.
  4. For bash shell, export $JAVA_HOME at ~/.bash_profile or ~/.bashrc.
  5. Test with echo $JAVA_HOME.
  6. Done.

bash or zsh?
On macOS 10.15 Catalina and later, the default Terminal shell switch from the bash (Bourne-again shell) to zsh (Z shell).

  • 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.

We can print the $SHELL environment variable to determine the current shell you are using.

Terminal

% echo $SHELL

/bin/zsh

What is /usr/libexec/java_home

On Mac OS X 10.5 or later, we can use /usr/libexec/java_home to return the location of the default JDK.

Terminal

% /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home  

Also, find all installed JDKs.

Terminal

% /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
    16 (x86_64) "Oracle Corporation" - "OpenJDK 16-ea" /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
    15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
    14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
    1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home  

Also, run a specified JDK command.

Terminal

% /usr/libexec/java_home -v1.8

/usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home  

$JAVA_HOME and macOS 11 Big Sur

On macOS 10.15 Catalina and later, the zsh is the default Terminal shell, and we can set the $JAVA_HOME environment variable in either ~/.zshenv or ~/.zshrc.

3.1 Open the ~/.zshenv

Terminal

% nano ~/.zshenv

3.2 Add the following content

~/.zshenv

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

3.3 Source the file and print the $JAVA_HOME, done.

Terminal

% source ~/.zshenv

% echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home  

$JAVA_HOME and Mac OS X 10.5 Leopard

For older Mac OS X, the bash is the default Terminal shell, and we can set the $JAVA_HOME environment variable in either ~/.bash_profile or ~/.bashrc.

Open the ~/.bash_profile

Terminal

% nano ~/.bash_profile

Add the following content

~/.bash_profile

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

Source the file and print the $JAVA_HOME

Terminal

% source ~/.bash_profile

% echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home  

$JAVA_HOME and older Mac OS X

On older Mac OS X, the tool /usr/libexec/java_home doesn’t exists, and we need to set the $JAVA_HOME to the real path.

Open the ~/.bash_profile

Terminal

% nano ~/.bash_profile

Add the following content

~/.bash_profile

export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Source the file and print the $JAVA_HOME

Terminal

% source ~/.bash_profile

% echo $JAVA_HOME
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

Switch between different JDK versions

For example, this macOS contains four JDK: 1.8, 14, 15, and 16, and the default JDK is 16.

Terminal

% /usr/libexec/java_home -V

Matching Java Virtual Machines (4):
  16 (x86_64) "Oracle Corporation" - "OpenJDK 16-ea" /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
  15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
  14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
  1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home

 

For zsh shell, edit the ~/.zshenv

Terminal

% nano ~/.zshenv

/usr/libexec/java_home -v"{$Version}" to activate a specified JDK version.

Add the following content to activate the JDK 1.8

~/.zshenv

export JAVA_HOME=$(/usr/libexec/java_home -v1.8)

If we want JDK 14.

~/.zshenv

export JAVA_HOME=$(/usr/libexec/java_home -v14)

If we want JDK 15.

~/.zshenv

export JAVA_HOME=$(/usr/libexec/java_home -v15)

Source the file and print the $JAVA_HOME, done.

Terminal


% source ~/.zshenv


% echo $JAVA_HOME
/usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home

Referance: you can read this article for more