Installing Java

In July 2018, Oracle released a new subscription-based pricing model for Java SE (Standard Edition) and announced that starting in January 2019, Java SE commercial users must buy a license to receive updates. The Oracle JDK LTS (Long Term Support), is released every three years.

An alternative JDK option is AdoptOpenJDK which is open source licensed and supports versions for longer, particularly the LTS-marked versions which are currently Java 8 and Java 11.

You can install your single preferred Java distribution, or if you wish or need to work with multiple JDKs then there are two alternatives for installing and managing versions: SDKMAN! and Homebrew.

SDKMAN!

SDKMAN! runs on any UNIX based platforms: Mac OSX, Linux, Cygwin, Solaris and FreeBSD and is used to install and manage Software Development Kits (SDKs) like Java.

The first step is to install SDKMAN!.

On Unix platforms simply open a new terminal and enter:

$ curl -s "https://get.sdkman.io" | bash

Also, then follow the instructions on-screen to complete the installation. Next, open a new terminal window or enter:

$ source "$HOME/.sdkman/bin/sdkman-init.sh"
To install SDKMAN! in Microsoft Windows, see the SDKMAN! Installation page for the details and options.

With the Next to see a list of available JDKs enter:

$ sdk list java

This shows something similar to:

 ================================================================================
 Available Java Versions
 ================================================================================
     12.ea.12-open
     11.0.3-open
     10.0.2-zulu
     10.0.2-open
     10.0.2-oracle
     9.0.7-zulu
     9.0.4-open
     8.0.202-zulu
     8.0.202-oracle
     7.0.202-zulu
     1.0.0-rc6-graal

 ================================================================================
 + - local version
 * - installed
 > - currently in use
 ================================================================================

To install Oracle JDK8: sdk install java 8.0.202-oracle and to install OpenJDK 11: sdk install java 11.0.3-open.

To switch between versions use either:

sdk use java 8.0.202-oracle
with sdk use java 11.0.3-open

And to set a default version use either:

sdk default java 8.0.202-oracle
sdk default java 11.0.3-open

After setting a default, rerunning the sdk list java command the report looks something like:

 ================================================================================
 Available Java Versions
 ================================================================================
     12.ea.12-open
 > * 11.0.3-open
     10.0.2-zulu
     10.0.2-open
     10.0.2-oracle
     9.0.7-zulu
     9.0.4-open
     8.0.202-zulu
   * 8.0.202-oracle
     7.0.202-zulu
     1.0.0-rc6-graal

 ================================================================================
 + - local version
 * - installed
 > - currently in use
 ================================================================================

Homebrew for macOS

To install Homebrew option a terminal window and enter:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Detailed instructions and options are found on the installation page for Homebrew.

To install Oracle JDK 8 enter the following:

macos$ brew tap caskroom/versions
macos$ brew cask install java8

And to install JDK 11 enter:

macos$ brew tap AdoptOpenJDK/openjdk
macos$ brew cask install adoptopenjdk11

The current list of AdoptOpenJDK version is found here.

To verify the installation and versions, enter:

macos$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    11.0.3, x86_64:	"AdoptOpenJDK 11"	/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_202, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

To manage multiple versions of the JDK there is a tool jEnv that aids in setting the current version of Java.

To install jEnv enter:

macos$ brew install jenv
macos$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile
macos$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

The next step is to make jEnv aware of the versions. First find where the versions are installed using /usr/libexec/java_home -V:

macos$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    11.0.3, x86_64:	"AdoptOpenJDK 11"	/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_202, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home

Next add them to jEnv with jenv add:

macos$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
openjdk64-11.0.3 added
11.0.3 added
11.0 added
macos$ jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
oracle64-1.8.0.202 added
1.8.0.202 added
1.8 added

The jenv versions command lists the versions jEnv is managing:

macos$ jenv versions
* system (set by /Users/dbm/.jenv/version)
  1.8
  1.8.0.202
  11.0
  11.0.3
  openjdk64-11.0.3
  oracle64-1.8.0.202

The jenv global command sets a global version and jenv local sets the JDK version locally, that is, per directory.

macos$ jenv global 11.0.3
macos$ jenv versions
  system
  1.8
  1.8.0.202
  11.0
* 11.0.3 (set by /Users/dbm/.jenv/version)
  openjdk64-11.0.3
  oracle64-1.8.0.202