James Schmidt

Senior Software Engineer at @wework, mountain biker, hiker

Home

Install OpenJDK 12 on macOS

Published Mar 23, 2019

Oracle recently invested work into making OpenJDK compatible with their commercial JDK. It is now worth using OpenJDK as an open source runtime for development and production environments.

Download

Download the macOS / x64 build from https://jdk.java.net/12/. It should download into your ~/Downloads directory.

Unzip the Package

Depending on your browser settings the downloaded package may already be unzipped. If not just double click the file in Finder and it will decompress the package contents as a directory.

Install

Copy the unzipped directory jdk-12.jdk to /Library/Java/JavaVirtualMachines/.

/Library/Java/JavaVirtualMachines$ ls
jdk-11.0.2.jdk	jdk-12.jdk

Notice that JDK 11 is there too. I can switch between these versions. More on switching versions later.

Environment

Next make sure the environment is configured. To be most compatible for all kinds of UNIX behavior on macOS I consolidate shell configuration to the .bashrc file and source it from the .bash_profile file.

.bash_profile

Set the .bash_profile file to source the .bashrc file in your home directory.

~$ cat .bash_profile
if [ -f ~/.bashrc ]; then
  source ~/.bashrc
fi

Set JAVA_HOME

Set the environment variable JAVA_HOME in the .bashrc file. Also include JAVA_HOME in the PATH environment variable.

~$ cat .bashrc
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH="$JAVA_HOME/bin:/usr/local/bin:$PATH"

Restart your shell and run java -version to see what version is loading.

$ java -version
openjdk version "12" 2019-03-19
OpenJDK Runtime Environment (build 12+33)
OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)

Switching Versions

Switching JDK versions is simple. Set the -v flag in the JAVA_HOME environment variable in the .bashrc file and restart your shell.

For Java 11

export JAVA_HOME=$(/usr/libexec/java_home -v 11)

For Java 12

By default the latest is used so specifying the latest version with the -v flag is optional.

export JAVA_HOME=$(/usr/libexec/java_home -v 12)

View Installed Versions

Run the following to see which versions of the JDK are installed. Running the command shows what JDK versions are installed and where.

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    12, x86_64:	"OpenJDK 12"	/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home
    11.0.2, x86_64:	"OpenJDK 11.0.2"	/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home

References