By default, macOS ships with a tool to resolve Java installations.
/usr/libexec/java_home
It searches in the following directories:
/Library/Java/JavaVirtualMachines/
~/Library/Java/JavaVirtualMachines/
Intellij will install downloaded jvms into the user's home directory, meaning we can use Intellij to manage and update our java installations. https://www.jetbrains.com/guide/java/tips/download-jdk/
By setting either JAVA_VERSION
or JAVA_HOME
, calling java
will point to the particular java version selected.
cwalker ~ % export JAVA_VERSION=21
cwalker ~ % java --version
openjdk 21.0.6 2025-01-21 LTS
OpenJDK Runtime Environment Corretto-21.0.6.7.1 (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM Corretto-21.0.6.7.1 (build 21.0.6+7-LTS, mixed mode, sharing)
For a faster way of switching in a shell, see this answer on stack overflow. https://stackoverflow.com/a/40754792