Here we will show you how to install Java on Mac OS.

Prerequisites:

This tutorial uses homebrew package manager to install Java, so that before going to start installing it, you must have installed homebrew on your Mac. We are assuming you have already installed. If not yet, homebrew can be install with a single command.

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

How to install Java on Mac OS:

Here are the simple steps to installing java on Mac OSX.

Update homebrew

$ brew update

Add homebrew cask

On Mac OSX homebrew is the package manager and homebrew cask is the app manager, it built on top of homebrew.

brew tap adoptopenjdk/openjdk

Search available jdks

brew search jdk

it provides all available jdks in adoptopenjdk/openjdk, similar like below.

==> Formulae
openjdk                                                                                                                         openjdk@11
==> Casks
adoptopenjdk                        adoptopenjdk11                      adoptopenjdk12-jre                  adoptopenjdk13-openj9               adoptopenjdk14-openj9-jre           adoptopenjdk15-openj9-jre-large     adoptopenjdk8-openj9-large
adoptopenjdk-jre                    adoptopenjdk11-jre                  adoptopenjdk12-openj9               adoptopenjdk13-openj9-jre           adoptopenjdk14-openj9-jre-large     adoptopenjdk15-openj9-large         adoptopenjdk9
adoptopenjdk-openj9                 adoptopenjdk11-openj9               adoptopenjdk12-openj9-jre           adoptopenjdk13-openj9-jre-large     adoptopenjdk14-openj9-large         adoptopenjdk8 ✔                     jdk-mission-control
adoptopenjdk-openj9-jre             adoptopenjdk11-openj9-jre           adoptopenjdk12-openj9-jre-large     adoptopenjdk13-openj9-large         adoptopenjdk15                      adoptopenjdk8-jre                   oracle-jdk
adoptopenjdk-openj9-jre-large       adoptopenjdk11-openj9-jre-large     adoptopenjdk12-openj9-large         adoptopenjdk14                      adoptopenjdk15-jre                  adoptopenjdk8-openj9                oracle-jdk-javadoc
adoptopenjdk-openj9-large           adoptopenjdk11-openj9-large         adoptopenjdk13                      adoptopenjdk14-jre                  adoptopenjdk15-openj9               adoptopenjdk8-openj9-jre            sapmachine-jdk
adoptopenjdk10                      adoptopenjdk12                      adoptopenjdk13-jre                  adoptopenjdk14-openj9               adoptopenjdk15-openj9-jre           adoptopenjdk8-openj9-jre-large

Choose jdk version and install

I am installing adoptopenjdk13 for testing.

$ brew install --cask adoptopenjdk13

Test it

Mac considers the latest version by default

$ java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)

Done!

Where is the Java installed:

Homebrew installs java at /Library/Java/JavaVirtualMachines/ directory. How ever you can see the installed location by giving the below command.

$ /usr/libexec/java_home -V       

Matching Java Virtual Machines (2):
    13.0.2, x86_64:	"AdoptOpenJDK 13"	/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home
    1.8.0_262, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/adoptopenjdk-13.jdk/Contents/Home

References:

Happy Learning 🙂