HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

How do I install Java on Mac OSX allowing version switching?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howjavainstallswitchingallowingosxversionmac

Problem

I want to install OpenJDK Java on Mac OSX and have it work alongside other JDK's since it is a newer release. Currently, I downloaded the tar.gz and placed it in my path but that is hard to maintain.

The only other install I found that do more things automatically is the install via Homebrew cask. It looks like only the current version too:

brew cask info java


Shows:


java: 13,33:5b8a42f3905b406298b72d750b6919f6

https://openjdk.java.net/

So I can install it from there, but then what? Am I stuck only with the new version?

Solution

Note: These solutions work for various versions of Java including Java 8 through Java 21 (the LTS version). This includes alternative JDK's from OpenJDK, Oracle, IBM, Azul, Amazon Correto, Graal and more.

You have a few options for how to do the installation as well as manage JDK switching. Installation can be done by Homebrew, SDKMAN, asdf, or a manual install. Switching can be done by SDKMAN, asdf, or manually by setting JAVA_HOME. All of these are described below.

TL;DR - Preferred Methods of Installation

You can install Java using whatever method you prefer including SDKMAN, asdf, Homebrew, or a manual install of the tar.gz file. The advantage of a manual install is that the location of the JDK can be placed in a standardized location for Mac OSX.

However, there are easier options such as SDKMAN and asdf that also will install other important and common tools for the JVM. These two primary options are described here.
Installing and Switching versions with SDKMAN

SDKMAN is a bit different and handles both the install and the switching. SDKMAN also places the installed JDK's into its own directory tree, which is typically ~/.sdkman/candidates/java. SDKMAN allows setting a global default version, and a version specific to the current shell.

-
Install SDKMAN from https://sdkman.io/install

-
List the Java versions available to make sure you know the version ID
sdk list java


-
Install one of those versions, for example, Java 21 LTS:
sdk install java 21-open


Or java 19:
sdk install java 19.0.2-open


-
Make Java 17 the default version:
sdk default java 17-open


Or switch to 17 for the current terminal session:
sdk use java 17-open


When you list available versions for installation using the list command, you will see a wide variety of distributions of Java:
sdk list java


And install additional versions, such as JDK 11 from Amazon:
sdk install java 11.0.14.10.1-amzn


SDKMAN can work with previously installed existing versions. Just do a local install giving your own version label and the location of the JDK:
sdk install java my-local-13 /Library/Java/JavaVirtualMachines/jdk-13.jdk/Contents/Home


And use it freely:
sdk use java my-local-13


SDKMAN will automatically manage your PATH and JAVA_HOME for you as you change versions. And as a note, it installs Java versions to ~/.sdkman/candidates/java/.

More information is available in the SDKMAN Usage Guide along with other SDK's it can install and manage such as Gradle, Maven, Kotlin, Quarkus, Spring Boot, and many others.

Installing and Switching versions with "asdf"

asdf is a version manager that supports installing and managing most languages, frameworks, and developer/devops tools. It has language specific plugins including one for Java.

-
First, install asdf via https://asdf-vm.com/guide/getting-started.html (read there to setup your shell correctly), or more simply:
brew reinstall asdf


and read the doc for setting up your shell correctly, but if you are using asdf from Homebrew with ZSH you can execute this command to finish setup:
echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc


-
Then install the Java plugin via https://github.com/halcyon/asdf-java
asdf plugin add java


and read the doc for setting up your shell correctly before continuing. Basically it says to add the following to your ~/.zshrc file (assuming you are not using another shell):
. ~/.asdf/plugins/java/set-java-home.zsh


-
Now list Java versions:
asdf list-all java


-
Install your favorite flavor and version:
asdf install java openjdk-21


or install the latest:
asdf install java latest


Other important commands are...

-
List your installed versions:
asdf list java


-
Set a global Java version:
asdf global java openjdk-21


-
Set a local Java version for a directory:
asdf local java openjdk-19


It's that easy! asdf will automatically manage your PATH and JAVA_HOME for you as you change versions. As a note, asdf installs Java versions to ~/.asdf/installs/java.

There are other languages and plugins for asdf here from the repository page: https://github.com/asdf-vm/asdf-plugins

Other Methods of Installation
Install with Homebrew

The version of Java available in Homebrew Cask previous to October 3, 2018 was indeed the Oracle JVM. Now, however, it has now been updated to OpenJDK. Be sure to update Homebrew and then you will see the lastest version available for install.

-
install Homebrew if you haven't already. Make sure it is updated:
brew update


-
Add the casks tap:
brew tap homebrew/cask-versions


These casks change their Java versions often, and there might be other taps out there with additional Java versions.

-
Look for installable versions:
brew search java


or for Eclipse Temurin versions:
brew search temurin


-
Check the details on the version that will be installed:
brew info java


or for the Temurin version:
` br

Context

Stack Overflow Q#52524112, score: 1212

Revisions (0)

No revisions yet.