Posted By: Anonymous
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
So I can install it from there, but then what? Am I stuck only with the new version?
Note: These solutions work for various versions of Java including Java 8, Java 11, and the new Java 15, and for any other previous Java version covered by the listed version managers. This includes alternative JDK’s from OpenJDK, Oracle, IBM, Azul, Amazon Correto, Graal and more. Easily work with Java 7, Java 8, Java 9, Java 10, Java 11, Java 12, Java 13, Java 14, and Java 15!
You have a few options for how to do the installation as well as manage JDK switching. Installation can be done by Homebrew, SDKMAN, Jabba, or a manual install. Switching can be done by JEnv, SDKMAN, Jabba, or manually by setting
JAVA_HOME. All of these are described below.
First, install Java using whatever method you prefer including Homebrew, SDKMAN 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. Otherwise, there are easier options such as SDKMAN that also will install other important and common tools for the JVM.
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 15:
sdk install java 15-open
Make 15 the default version:
sdk default java 15-open
Or switch to 15 for the session:
sdk use java 15-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 8:
sdk install java 8.0.181-oracle
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
JAVA_HOME for you as you change versions.
Install manually from OpenJDK download page:
Unarchive the OpenJDK tar, and place the resulting folder (i.e.
jdk-15.jdk) into your
/Library/Java/JavaVirtualMachines/folder since this is the standard and expected location of JDK installs. You can also install anywhere you want in reality.
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:
Add the casks tap, if you want to use the AdoptOpenJDK versions (which tend to be more current):
brew tap adoptopenjdk/openjdk
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 AdoptOpenJDK versions:
brew search jdk
Check the details on the version that will be installed:
brew info java
or for the AdoptOpenJDK version:
brew info adoptopenjdk
Install a specific version of the JDK such as
adoptopenjdk13, or just
adoptopenjdkfor the most current of that distribution. For example:
brew install java brew install --cask adoptopenjdk15
And these will be installed into
/Library/Java/JavaVirtualMachines/ which is the traditional location expected on Mac OSX.
Other installation options:
Some other flavours of OpenJDK are:
Zulu® is a certified build of OpenJDK that is fully compliant with the Java SE standard. Zulu is 100% open source and freely downloadable. Now Java developers, system administrators, and end-users can enjoy the full benefits of open source Java with deployment flexibility and control over upgrade timing.
Amazon Correto OpenJDK builds have an easy to use an installation package for Java 8 or Java 11, and installs to the standard
/Library/Java/JavaVirtualMachines/ directory on Mac OSX.
Amazon Corretto is a no-cost, multiplatform, production-ready distribution of the Open Java Development Kit (OpenJDK). Corretto comes with long-term support that will include performance enhancements and security fixes. Amazon runs Corretto internally on thousands of production services and Corretto is certified as compatible with the Java SE standard. With Corretto, you can develop and run Java applications on popular operating systems, including Linux, Windows, and macOS.
Where is my JDK?!?!
To find locations of previously installed Java JDK’s installed at the default system locations, use:
Matching Java Virtual Machines (8):
15, x86_64: "OpenJDK 15" /Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home
14, x86_64: "OpenJDK 14" /Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home
13, x86_64: "OpenJDK 13" /Library/Java/JavaVirtualMachines/openjdk-13.jdk/Contents/Home
12, x86_64: "OpenJDK 12" /Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
11, x86_64: "Java SE 11" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
10.0.2, x86_64: "Java SE 10.0.2" /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
9, x86_64: "Java SE 9" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
1.8.0_144, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
You can also report just the location of a specific Java version using
-v. For example for Java 15:
/usr/libexec/java_home -v 15
Knowing the location of the installed JDK’s is also useful when using tools like JEnv, or adding a local install to SDKMAN, or linking a system JDK in Jabba — and you need to know where to find them.
If you need to find JDK’s installed by other tools, check these locations:
- SDKMAN installs to
- Jabba installs to
Switching versions manually
The Java executable is a wrapper that will use whatever JDK is configured in
JAVA_HOME, so you can change that to also change which JDK is in use.
For example, if you installed or untar’d JDK 15 to
/Library/Java/JavaVirtualMachines/jdk-15.jdk if it is the highest version number it should already be the default, if not you could simply set:
And now whatever Java executable is in the path will see this and use the correct JDK.
/usr/libexec/java_home utility as previously described helps you to create aliases or to run commands to change Java versions by identifying the locations of different JDK installations. For example, creating shell aliases in your
.bash_profile to change
JAVA_HOME for you:
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8) export JAVA_9_HOME=$(/usr/libexec/java_home -v9) export JAVA_10_HOME=$(/usr/libexec/java_home -v10) export JAVA_11_HOME=$(/usr/libexec/java_home -v11) export JAVA_12_HOME=$(/usr/libexec/java_home -v12) export JAVA_13_HOME=$(/usr/libexec/java_home -v13) export JAVA_14_HOME=$(/usr/libexec/java_home -v14) export JAVA_15_HOME=$(/usr/libexec/java_home -v15) alias java8='export JAVA_HOME=$JAVA_8_HOME' alias java9='export JAVA_HOME=$JAVA_9_HOME' alias java10='export JAVA_HOME=$JAVA_10_HOME' alias java11='export JAVA_HOME=$JAVA_11_HOME' alias java12='export JAVA_HOME=$JAVA_12_HOME' alias java13='export JAVA_HOME=$JAVA_13_HOME' alias java14='export JAVA_HOME=$JAVA_14_HOME' alias java15='export JAVA_HOME=$JAVA_15_HOME' # default to Java 15 java15
Then to change versions, just use the alias.
java8 java -version
java version "1.8.0_144"
Of course, setting
JAVA_HOME manually works too!
Switching versions with JEnv
JEnv expects the Java JDK’s to already exist on the machine and can be in any location. Typically you will find installed Java JDK’s in
/Library/Java/JavaVirtualMachines/. JEnv allows setting the global version of Java, one for the current shell, and a per-directory local version which is handy when some projects require different versions than others.
Install JEnv if you haven’t already, instructions on the site http://www.jenv.be/ for manual install or using Homebrew.
Add any Java version to JEnv (adjust the directory if you placed this elsewhere):
jenv add /Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home
Set your global version using this command:
jenv global 15
You can also add other existing versions using
jenv add in a similar manner, and list those that are available. For example Java 8:
jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home jenv versions
See the JEnv docs for more commands. You may now switch between any Java versions (Oracle, OpenJDK, other) at any time either for the whole system, for shells, or per local directory.
To help manage
JAVA_HOME while using JEnv you can add the export plugin to do this for you.
$ jenv enable-plugin export You may restart your session to activate jenv export plugin echo export plugin activated
The export plugin may not adjust
JAVA_HOME if it is already set, so you may need to clear this variable in your profile so that it can be managed by JEnv.
You can also use
jenv exec <command> <parms...> to run single commands with
PATH set correctly for that one command, which could include opening another shell.
Installing and Switching versions with Jabba
Jabba also handles both the install and the switching. Jabba also places the installed JDK’s into its own directory tree, which is typically
Install Jabba by following the instructions on the home page.
List available JDK’s
Install Java JDK 12
jabba install [email protected]
jabba use [email protected]
You can also alias version names, link to existing JDK’s already installed, and find a mix of interesting JDK’s such as GraalVM, Adopt JDK, IBM JDK, and more. The complete usage guide is available on the home page as well.
Jabba will automatically manage your
JAVA_HOME for you as you change versions.