Homebrew is a package manager that makes it easy for us to install all sorts of development tools such as MySQL, Mongo, Ruby on Rails, and Java. Below are the steps required to install Java using Homebrew.

Step 0 - Install Homebrew

Homebrew does not come pre-installed with Mac, so you'll have to install it yourself. Thankfully, it's a single line of code that you paste into the terminal. Here are instructions on how to install Homebrew on Mac.

Approach #1 - Install Up-To-Date JDK

Update 10/24/20: java was migrated from homebrew/cask to homebrew/core.

This approach will

brew install java

Approach #2 - Install Directly from AdoptOpenJDK

Some projects require an older version of JDK (such as Java 8). The best thing to do is install AdoptOpenJDK.

brew tap adoptopenjdk/openjdk

Install Java 8

brew cask install adoptopenjdk8

Install Java 12

brew cask install adoptopenjdk12

Step 3 - Verify Your Work

brew info java

Troubleshooting

If brew keeps erroring out for no reason, I suggest running this command.

brew cleanup && brew cleanup

Resources