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

  • A+
Category:Languages

I want to install OpenJDK Java 11 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 installs I found that do more things automatically are the Oracle install via Homebrew cask, but that is the commercial JDK and I want the open-source one!

brew cask info java 

Shows:

java: 11,28:55eed80b163941c8885ad9298e6d786a https://www.oracle.com/technetwork/java/javase/overview/index.html

Which it is the Oracle JDK and not OpenJDK, see: https://discourse.brew.sh/t/how-to-install-openjdk-with-brew/712

Following directions from an older question (Mac OS X and multiple Java versions) is outdated and gives me the wrong version -- Oracle not OpenJDK.

Note: this question is intentionally written and answered by the author (Self-Answered Questions), so that the idiomatic answers to commonly asked topics are present in SO.

 


note: These solutions work for your specific case of Java 11 OpenJDK but also work for any other previous Java version covered by the listed version managers. This includes alternative JDK's from OpenJDK, Oracle, IBM, Azul, Graal and more. Easily work with Java 7, Java 8, Java 9, Java 10, and now 11.

You have a few options of how to do installation as well as manage switching. Installation can be done by Homebew, SDKMAN, Jabba, or a manual install. Switching can be done by JEnv, SDKMAN, or Jabba.


Installation

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

Install with SDKMAN

This is a simple model in that it handles both installation and version switching, with a caveat that it installs the JDK into a non-standard directory.

<see below "Installing and Switching versions with SDKMAN">

Install using Jabba

This is also a simple model in that both installation and version switching are handled by the same tool. The installations are made to a non-standard directory.

<see below "Installing and Switching versions with Jabba">

Install manually from OpenJDK download page:

  1. Download OpenJDK 11 for Mac OSX from http://jdk.java.net/11/

  2. Unarchive the OpenJDK tar, and place the resulting jdk-11.jdk folder 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

<not yet available for OpenJDK 11, coming soon! from AdoptOpenJdk>

For other versions of Java:

  1. install Homebrew if you haven't already.

  2. Add the casks tap:

    brew tap homebrew/cask-versions 
  3. Look for installable versions:

    brew search java 
  4. Check the details on the version that will be installed:

    brew cask info java 

    Note that the current release is the Oracle commercial version of Java 11, and not OpenJDK 11.

  5. Install a specific version of the JDK such as java8, java10 or java for the current:

    brew cask install java 

And these will be installed into /Library/Java/JavaVirtualMachines/ which is the traditional location expected on Mac OSX.

Other installation options:

You can also install Azul Systems Java Zulu certified builds of OpenJDK by following the instructions on their site.

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.


Where is my JDK?!?!

To find locations of previously installed Java JDK's installed at the default system locations, use:

/usr/libexec/java_home -V 

This is 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 instaled by other tools, check these locations:

  • SDKMAN installs to ~/.sdkman/candidates/java/
  • Jabba installs to ~/.jabba/jdk

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.

  1. Install JEnv if you haven't already, instructions on the site http://www.jenv.be/ for manual install or using Homebrew.

  2. Add any Java version to JEnv (adjust the directory if you placed this elsewhere):

    jenv add /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home 
  3. Set your global version using this command:

    jenv global 11 

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 JAVA_HOME and PATH set correctly for that one command, which could include opening another shell.


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.

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

  2. Install Java 11 using SDKMAN:

    sdk install java 11.0.0-open  
  3. Make 11 the default version:

    sdk default java 11.0.0-open 

    Or switch to 11 for the session:

    sdk use java 11.0.0-open 

You can list available versions for installation using the list command:

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-11 /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home 

And use it freely:

sdk use java my-local-11 

More information is available in the SDKMAN Usage Guide along with other SDK's it can install and manage.

SDKMAN will automatically manage your PATH and JAVA_HOME for you as you change versions.

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 ~/.jabba/jdk.

  1. Install Jabba by following the instructions on the home page.

  2. List available JDK's

    jabba ls-remote 
  3. Install Java JDK 11

    jabba install openjdk@1.11.0 
  4. Use it:

    jabba use openjdk@1.11.0 

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 PATH and JAVA_HOME for you as you change versions.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: