Skip to content

Instantly share code, notes, and snippets.

@tinkerware
Last active November 11, 2024 09:25
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.

@edreyer
Copy link

edreyer commented Sep 25, 2018

slightly cleaner version:

###############
# Java Switcher
###############
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"

# Set java 8 as default
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

@matiasiglesias
Copy link

NEW as of June 2019

To install the JDKs from AdoptOpenJDK:

brew tap adoptopenjdk/openjdk

brew cask install adoptopenjdk8
brew cask install adoptopenjdk9
brew cask install adoptopenjdk10
brew cask install adoptopenjdk11

@outrowender
Copy link

outrowender commented Nov 4, 2021

NEW as of November 2021

brew tap adoptopenjdk/openjdk

brew install --cask adoptopenjdk8

and then, export it in your .zshrc file

export JAVA_HOME="/usr/libexec/java_home -v 1.8"

@joshuapinter
Copy link

Java 17 via brew on an M1 Mac

After installing via brew on an M1 Mac, mine was located:

/opt/homebrew/Cellar/openjdk/17.0.1_1/libexec/openjdk.jdk/Contents/Home

Obviously change for your version but the typical methods of finding the path proved difficult so I hope this helps others.

@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