Installing Java 11 on MacOS with HomeBrew

Prerequisite: HomeBrew

Before we start

Find locations of previously installed Java JDK:

$ /usr/libexec/java_home -VMatching Java Virtual Machines (1): 1.8.0_162, x86_64: “Java SE 8” /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home

Find the location of a specific Java version using -v:

$ /usr/libexec/java_home -v 1.8/Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home

Install with HomeBrew

  1. Update HomeBrew.

$ brew update

2. Add the casks tap.

$ brew tap homebrew/cask-versions

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

$ brew cask install java

Switching versions

Add the following aliases to .bash_profile.

export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)export JAVA_11_HOME=$(/usr/libexec/java_home -v11)alias java8=’export JAVA_HOME=$JAVA_8_HOME’alias java11=’export JAVA_HOME=$JAVA_11_HOME’# default to Java 11java11

Reload .bash_profile for the aliases to take effect.

$ source ~/.bash_profile

Use the alias to change version.

$ java8$ java -versionjava version “1.8.0_162″Java(TM) SE Runtime Environment (build 1.8.0_162-b12)Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

Full explanation by Jayson Minard: https://stackoverflow.com/a/52524114/5366727

23