Installing JDK on MacOS

  01 Jun 2019


This is a very brief article on how I installed OpenJDK on MacOS. The easiest way to install JDK on MacOS is via package manager Homebrew.

## Installation

Make sure you have Homebrew installed.

To install the latest OpenJDK run the following command in the Terminal:

brew cask install java

JDK will be installed in /Library/Java/JavaVirtualMachines/, e.g.:

/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/

As long as any JDK is moved to this location, it should automatically register with the OS. There is no need to run another command to register JDK with the OS.

How to list the current active JDK on the system level:

$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home

How to list all installed JDKs:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    11.0.2, x86_64:	"OpenJDK 11.0.2"	/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
    1.8.0_212, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Installing a specific version

The older Oracle JDKs are not available via Homebrew any more. Use OpenJDK instead. To get access to the older OpenJDK versions, you have to add an external repo to Homebrew:

# add open jdk repo
brew tap adoptopenjdk/openjdk
# see available versions
brew search openjdk
# install specific version
brew cask install adoptopenjdk8

Switching Java Versions

# see available java versions
/usr/libexec/java_home -V
# pick a specific version - no need to specify all the version details:
# 1.8.0_05 vs 1.8
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

Add the last line to .bash_profile. Then run:

source ~/.bash_profile

Important: This change doesn’t make this version available system wide, meaning, quite likely standard apps still won’t pick up this particular version, only scripts you execute from the Terminal. To work around this, you can open an app from the Terminal like so:

open -a "Neo4j Desktop"

You could also go a step further and create aliases to switch between versions, e.g.:

alias j11="export JAVA_HOME=/usr/libexec/java_home -v 11.0.2; java -version"
alias j8="export JAVA_HOME=/usr/libexec/java_home -v 1.8; java -version"

Sources:

comments powered by Disqus