Setting the default Java version on macOS

Java is well integrated into macOS. While the JRE or JDK is not part anymore of macOS itself like it was in the past and stopped with Java 6, interfaces, commands and standards to make it simple to use are still integrated by default and continued to be maintained.
Current Macs are including ARM64 CPUs (aka M1, M2, M3, aarch64, arm64) and not Intel CPUs (x86_64). You can still run x86_64 applications on M1/M2 Macs, also Zulu for x86_64, but recommended is to use Zulu for arm64 to achieve highest performance. The technical terms aarch64 and arm64 are just different words for the same CPU architecture.

Version detail display

While the java -version command shows the correct version numbers, it won't display the CPU architecture the JRE was built for. Use java -Xinternalversion to display the CPU type. Example: 

% java -Xinternalversion
OpenJDK 64-Bit Server VM (17.0.7+7-LTS) for bsd-aarch64 JRE (17.0.7+7-LTS) \
(Zulu17.42+19-CA), built on Apr 10 2023 06:05:36 by "zulu_re" [...]

Listing all installed JREs and JDKs

The /usr/libexec/java_home command lists all JREs and JDKs which have been installed by DMG / PKG packages into the default /Library/Java location. It also shows the CPU architecture.

% /usr/libexec/java_home -V 
Matching Java Virtual Machines (5):
20.0.1 (arm64) "Azul Systems, Inc." - "Zulu 20.30.11" /Library/Java/JavaVirtualMachines/zulu-20.jdk/Contents/Home
19.0.2 (arm64) "Azul Systems, Inc." - "Zulu 19.32.13" /Library/Java/JavaVirtualMachines/zulu-19.jdk/Contents/Home
17.0.7 (arm64) "Azul Systems, Inc." - "Zulu 17.42.19" /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
11.0.19 (arm64) "Azul Systems, Inc." - "Zulu 11.64.19" /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
1.8.0_372 (arm64) "Azul Systems, Inc." - "Zulu 8.70.0.23" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/zulu-20.jdk/Contents/Home

Setting the default Java version

Most Java-based applications today on macOS have their JRE included into the application DMG / PKG package. A few don't  and offer their own configuration dialog to select from the installed JREs or JDKs. For those applications which you start from the macOS Terminal.app, for example as developer or when only downloading a single .jar file, you can define the Java version to run it on as follows:

By default, the macOS built-in Java launcher command /usr/bin/java selects the highest installed Java version number.

To change that, set the shell environment variable JAVA_VERSION.

Example on a Mac where Zulu 11, Zulu 17 and Zulu 20 are installed. By default Zulu 20 would be chosen when just running java or /usr/bin/java and here we change it to Zulu 11:

% export JAVA_VERSION=11
% java -version
openjdk version "11.0.19" 2023-04-18 LTS
OpenJDK Runtime Environment Zulu11.64+19-CA (build 11.0.19+7-LTS)
OpenJDK 64-Bit Server VM Zulu11.64+19-CA (build 11.0.19+7-LTS, mixed mode)

To persist that setting, add the line defining JAVA_VERSION to your shell start script, for example $HOME/.zshrc on current macOS.

Alternatively you can also set JAVA_HOME as that is also evaluated by the /usr/bin/java launcher command:

% export JAVA_HOME=$(/usr/libexec/java_home -v 11)
% java -version
openjdk version "11.0.19" 2023-04-18 LTS
OpenJDK Runtime Environment Zulu11.64+19-CA (build 11.0.19+7-LTS)
OpenJDK 64-Bit Server VM Zulu11.64+19-CA (build 11.0.19+7-LTS, mixed mode)
In case you want to select Java 8 use JAVA_VERSION=1.8 because JAVA_VERSION=18 would select Java 18 or higher. Debug output can be activated by setting JAVA_LAUNCHER_VERBOSE=1.

Add Comment

Comments

0 comments

Please sign in to leave a comment.

Was this article helpful?
0 out of 0 found this helpful