Skip to content

Instantly share code, notes, and snippets.

@tinkerware
Last active October 19, 2023 08:22
Show Gist options
  • Save tinkerware/8d92524d78f958f3d821b127393a96a1 to your computer and use it in GitHub Desktop.
Save tinkerware/8d92524d78f958f3d821b127393a96a1 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.

@jbcrestot
Copy link

jbcrestot commented Sep 15, 2023

Installing on M1 & Ventura 13.5

brew install --cask adoptopenjdk8
==> Caveats
Temurin is the official successor to this software:

brew install --cask temurin8

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

java 8

brew tap buo/cask-upgrade & brew tap caskroom/versions
brew install --cask temurin8
# then 
echo export "JAVA_HOME=\$(/usr/libexec/java_home -v 1.8)" >> ~/.zshrc

java 11

brew tap adoptopenjdk/openjdk
brew install --cask adoptopenjdk11
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.zshrc

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