Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bard86/f5dd8b5312007ca2bcb327199b403091 to your computer and use it in GitHub Desktop.
Save bard86/f5dd8b5312007ca2bcb327199b403091 to your computer and use it in GitHub Desktop.
OpenJDK 11+ on MacOS X

OpenJDK 11+ on MacOS X

Manually downloading, extracting and configuring the installation of OpenJDK 11+ is a high-maintenance exercise. Particularly if you need to install and switch between multiple versions of the JDK.

The following options for installing OpenJDK 11+ and switching between versions make the job easier..

Install with Jabba

Jabba is a Java version manager inspired by nvm (Node.js) written in Go.

  1. Install Jabba if you don't have it already
curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh
  1. The list of available JDK can be displayed
jabba ls-remote

1.11.0
1.10.0
1.10.0-2
1.8.181
1.6.65
[email protected]
[email protected]
....
  1. OpenJDK 11 can be installed with
jabba install [email protected]
  1. Switch to a specific version with
jabba use [email protected]

Jabba automatically manages JAVA_HOME and PATH settings. It installs JDKs to ~/.jabba/jdk.

Install with SDKMAN

SDKMAN is a tool for running multiple development kits on a machine. It can manage the installation and switching of Java versions.

  1. Install SDKMAN if you don't have it already
url -s "https://get.sdkman.io" | bash

Follow the instructions on-screen.

Next, run the following

source "$HOME/.sdkman/bin/sdkman-init.sh"
  1. The different available versions of Java can be listed
sdk list java
  1. Install the OpenJDK 11 with
sdk install java 11.0.0-open 
  1. Make OpenJDK 11 the default version
sdk default java 11.0.0-open

or switch to a different version for a particular session

sdk use java 11.0.0-open

Install with Homebrew

Before 3rd October 2018 the Oracle Java JDK was available via Homebrew. It has now been updated to OpenJDK.

  1. Install Homebrew if you don't have it already. Ensure it is updated
brew update
  1. Add the casks tap
brew tap homebrew/cask-versions
  1. Search for installable versions
brew search java
  1. Verify the details of the version that will be installed
brew cask info java
  1. Install a specific version java8, java10 or the latest java
brew cask install java

The AdoptOpenJDK is also available from Homebrew Cask

brew cask install adoptopenjdk

The installation directory is /Library/Java/JavaVirtualMachines/ which is the traditional location on MacOSX

To find the locations of installed JDKs

/usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    11, x86_64:	"OpenJDK 11"	/Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home
    1.8.0_45, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home

or for a specific version

/usr/libexec/java_home -v 11
/Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home

Note the upper and lowercase 'v'.

The /Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home utility can be to change versions manually or add aliases for a particular shell. The following can be added to .bash_profile or .profile

export JAVA8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA9_HOME=$(/usr/libexec/java_home -v9)
export JAVA10_HOME=$(/usr/libexec/java_home -v10)
export JAVA11_HOME=$(/usr/libexec/java_home -v11)

alias java8='export JAVA_HOME=$JAVA8_HOME'
alias java9='export JAVA_HOME=$JAVA9_HOME'
alias java10='export JAVA_HOME=$JAVA10_HOME'
alias java11='export JAVA_HOME=$JAVA11_HOME'

# Specify Java 11 as the default version
java11

The aliases can be used to change versions on the fly

java8

The Java version can be checked with

java -version

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment