If you’ve just moved from Windows to macOS or starting fresh on your new Mac and wondering how to manage multiple versions of Java Development Kit (JDK) such as 8, 17, 21 etc system wide then read on.
/usr/libexec/java_home – the simplest way
There’s a folder inside /usr called libexec that will be able to help you achieve a quick switch of your JDK system wide without relying on any external package managers or sdk managers. Let’s assume you have JDK 17 already installed system wide and you downloaded the DMG or PKG file for JDK 21 and had it installed. Yet, when you fire up a terminal and issue java -version you still see version 17 and not 21! So where did the installed files go?
Well, most JDK installers on macOS will install the files to /Library/Java/JavaVirtualMachines folder and add an entry to /usr/libexec/java_home utility for the newly installed version.
So if you fire up a terminal and issue /usr/libexec/java_home -V (capital V) you will see a list of installed JDKs available on your macOS. On mine it looks like the following
Matching Java Virtual Machines (3):
21.0.2 (arm64) "Azul Systems, Inc." - "Zulu 21.32.17" /Library/Java/JavaVirtualMachines/zulu-21.jdk/Contents/Home
17.0.4.1 (arm64) "Eclipse Adoptium" - "OpenJDK 17.0.4.1" /Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home
1.8.0_392 (arm64) "Azul Systems, Inc." - "Zulu 8.74.0.17" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/zulu-21.jdk/Contents/Home
I have three versions of JDK installed on my machine – 8, 17 and 21. The last line of the output shows that currently java_home is pointing to version 21 on my machine. This is easily verified by issuing java -version which gives the following output
openjdk version "openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Zulu21.32+17-CA (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Zulu21.32+17-CA (build 21.0.2+13-LTS, mixed mode, sharing)
If you need to switch to a different version of Java, then simply issue the following command replacing the version at the end with your desired version.
export JAVA_HOME=`/usr/libexec/java_home -v 17`
You don’t need to type the full version. The closest match will be returned by -v and will be exported to JAVA_HOME environment variable. If you issue java -version you will see that it’s now pointing to version 17 and not 21.
Remember that this change will be temporary to the terminal you have issued the command on. If you need to make this permanent, then you will need to add this command to .zshrc file in your user folder if you’re on macOS 10.5 or later. If however, you’re on macOS before 10.5 then you’ll have to add this command to .bashrc-profile file in your user folder.


One response to “Managing multiple JDKs on macOS”
[…] everytime you need to switch JDK version or you could also simply use /usr/libexec/java_home (see this article) however, a more elegant and simpler way is to install and use […]