How to switch between Java LTS versions 8, 11, and 17 on Mac

Lothar Schulz
Miro Engineering
Published in
2 min readNov 25, 2021

At Miro we use Java quite extensively. Also we like to share our reflections and engage with the community. While focusing on one Java version at Miro, we’d like to share in this write-up how to switch between Java/JDK versions on Mac via the command line.

On Mac you can install Java/JVM with brew’s openjdk formulae. That includes JAVA LTS releases 17, 11, 8:

# version 17
brew install openjdk@17
sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
# version 11
brew install openjdk@11
sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
# version 8
brew install openjdk@8
sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk

Setting the symlink after every installation step is important for the system Java wrappers to find the installed JDK. Installing JDKs with brew also recommends adding /usr/local/opt/openjdkXX/bin to PATH.

javahome function

I prefer the javahome shell function below rather than adding new values to PATH.

javahome() {
unset JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home -v "$1");
java -version
}
alias j1.8='javahome 1.8'
alias j11='javahome 11'
alias j17='javahome 17'

Make sure to add the javahome function to your shell init file (.zshrc for me) so it is available in all terminal sessions.

javahome function in action

Now you can change the java version on the terminal using the respective alias:

$ j17
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment Homebrew (build 17.0.1+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.1+0, mixed mode, sharing)
$ j11
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment Homebrew (build 11.0.12+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.12+0, mixed mode)
$ j1.8
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-bre_2021_10_20_23_15-b00)
OpenJDK 64-Bit Server VM (build 25.312-b00, mixed mode)

Join our team!

Would you like to be an Engineer at Miro? Checkout out opportunities to join the Engineering team.

--

--