Keywords: Java, JDK (Java Development Kit), MacOS, Homebrew
This how-to guide covers how to install different versions of the JDK on MacOS with Homebrew.
This guide favors OpenJDK
This guide favors free, simple, and permissive licensing whenever possible, so we will favor OpenJDK over Oracle JDK because OpenJDK is licensed GNU GPL with a linking exception. For more information, see Oracle now requires a subscription to use Java SE (2018).
- For Java 8 and later, we will install OpenJDK.
- For anything prior to Java 8 or other special circumstances, anything goes. We cover how to install whatever is most readily available, whether that's Oracle or OpenJDK.
Install Homebrew.
Double check your registered taps by running brew tap
.
It's generally good practice to have homebrew/cask
and homebrew/cask-versions
taprooms tapped, especially installing multiple Java versions. If homebrew/cask
and homebrew/cask-versions
aren't in the list of your registered taps, then run brew tap homebrew/cask
and brew tap homebrew/cask-versions
to tap them.
$ brew tap # Check taps
$ brew tap homebrew/cask # Tap into cask
$ brew tap homebrew/cask-versions # Tap into cask-versions
Remember to frequently brew update
.
$ brew update
A lot of the information in this how-to guide was simply gathered from brew search
and brew info
. If you're still getting the hang of things, I highly recommend you get the overview of what's available by trying these commands out yourself.
Java/JDK formula/cask names typically contain either java
or jdk
. Let's brew search
for java
and jdk
to see potential formulas.
$ brew search java
We can see some of the formulae/casks we're looking for, such as java
, java11
, or java-beta
.
$ brew search jdk
We can see some more potential formulae/casks we're looking for, such as openjdk
, adoptopenjdk8
, and oracle-jdk
.
Get the metadata for a formula/cask you are interested by running brew info <tap>
or brew cask info <cask>
.
As an example, let's start simple and get the info of java
.
$ brew info java # At time of writing, shows metadata for OpenJDK 13
$ brew cask info java # At SAME time of writing, shows metadata for OpenJDK 14. The cask may differ from the formula!
- Note that
java
is formula is available onhomebrew/core
. You don't need to usebrew cask
or tap any caskrooms. - Note that
java
is also a cask. The formula and the cask version might differ.
$ brew info java # Double check information about the formula, such as JDK build version and OpenJDK.
$ brew install java # Install the most recent stable feature release of OpenJDK
The java-beta
cask contains the Early Access OpenJDK.
$ brew cask info java-beta # Double check information about the cask, such as JDK build version and OpenJDK.
$ brew cask install java-beta # Install the early access feature release of the OpenJDK. At time of writing, OpenJDK 15.
TODO - Most likely involving having cask-versions
tapped and/or using a syntax like brew cask install java@12
. Comment if you have a working solution 😜.
JDK 11 is an LTS (Long Term Support) version.
- At time of writing, there is no such thing as a
java-lts
formula or cask. You must explicitly choose to install Java 11. - Note that this formula is available on
homebrew/core
, so you don't need to have tapped any caskrooms.
$ brew info java11 # Confirm information about the formula. Note that it's poured from the OpenJDK formula.
$ brew install java11 # Install OpenJDK 11
$ brew tap homebrew/cask-versions # Tap the cask-versions taproom to get access to different cask versions.
$ brew search jdk # Find `adoptopenjdk8` in the casks
$ brew cask info adoptopenjdk8 # Confirm information about the cask. Note that this is an AdoptOpenJDK build of OpenJDK.
$ brew cask install adoptopenjdk8 # Install the AdoptOpenJDK build of OpenJDK8
See the AdoptOpenJDK HomeBrew Tap Github Repo for an alternative taproom with more AdoptOpenJDK versions.
- 💀OBSOLETE💀: It appears Java 7 is no longer available in
homebrew/cask-versions
. It used to be available atcaskroom/versions/java7
.
# THIS IS OBSOLETE. INCLUDED FOR REFERENCE PURPOSES.
$ brew cask info caskroom/versions/java7 # Query info about Java cask installation beforehand
$ brew cask install caskroom/versions/java7 # For Java 7
If you know how to find Java 7 on Homebrew, please leave a comment on how to do so and we can add it in here. 😜
- Note from the metadata that this is JDK is for Apple OSX.
$ brew tap homebrew/cask-versions # Make sure you have cask-versions tapped
$ brew cask info java6 # Double check information about the cask.
$ brew cask install java6 # Install Java6 for OSX
- MacOS Java install location will be at
/Library/Java/JavaVirtualMachines/
. - When upgrading Java installations with Homebrew
- Be aware upgrades will overwrite the
cacerts
truststore file (if you have edited that) - Be aware that the JDK install directory's name may fall out of date when you perform the upgrade. You may have to manually rename the directory, and make tweaks to your configurations accordingly.
- Be aware upgrades will overwrite the
- I recommend jEnv for switching between multiple Java environments on MacOS.
- I think it's generally wise to have
homebrew/cask
andhomebrew/cask-versions
tapped. They are practically core.