Skip to content

Instantly share code, notes, and snippets.

@mpslanker
Forked from tinkerware/macos-jdk-install.md
Created August 18, 2022 22:48
Show Gist options
  • Save mpslanker/534edb8e3dfdfa5e2add29fa12fa5a03 to your computer and use it in GitHub Desktop.
Save mpslanker/534edb8e3dfdfa5e2add29fa12fa5a03 to your computer and use it in GitHub Desktop.
Maintaining Java Installs on macOS Using Homebrew Cask

Maintaining Java Installs on macOS Using Homebrew Cask

Recently, I upgraded my MacBook Pro from a old, trusty Yosemite to Sierra, and reluctantly had to clean out the old JDK versions I had accumulated over a few years. I also wanted to have a Java 9 JDK to play around with the new module system and API’s.

Good news is that, for a while now, you have been able to install and upgrade multiple versions of JDK using only your shell, without having to deal with Oracle’s graphical installers.

To install Java from scratch, install Homebrew Cask cask-update (you need to have Homebrew already installed) first, then install Java using Cask:

brew tap buo/cask-upgrade & brew tap caskroom/versions
brew cask install java8

To install latest version of Java 9, run brew cask install java.

Choosing between different Java versions

The simple option is to set JAVA_HOME to symbolic link /Library/Java/Home, then point it to you desired JDK install:

$ export JAVA_HOME="/Library/Java/Home"
$ sudo ln -nsf $(/usr/libexec/java_home -v 1.8) $JAVA_HOME
$ java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
$ sudo ln -nsf $(/usr/libexec/java_home -v 9) $JAVA_HOME
$ java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

Updating to a new version of JDK

Run brew cu java or brew cu java8 if you want to upgrade the JDK for current version of JDK or for Java 8 respectively. The most recent successful upgrade points the symbolic link at /Library/Java/Home to itself, so you don’t need to update it manually unless you want to switch to a different major version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment