How to install java on mac using homebrew?

Homebrew is a software package manager for Mac operating system and also for Linux. It is free and open-source. Homebrew is entirely community-driven and managed, upgraded by the large community. There are many ways we can install java in the Mac operating system. The most convenient way of installing software in Mac is homebrew. Following are the steps to install java in Mac:

Assuming you have the latest brew installed in your Mac.

Step1: Look for the installable java versions

First of all we can look for the installable java versions that we can install in our Mac. To see the installable versions type the following command:

$ brew search java

The above command will show something similar to the following output:

==> Formulae
app-engine-java           google-java-format        javarepl                  jslint4java               libreadline-java

==> Casks
charles-applejava                           java6                                       homebrew/cask/netbeans-java-ee
java-beta                                   homebrew/cask/eclipse-java                  homebrew/cask/netbeans-java-se
java11                                      homebrew/cask/java                          homebrew/cask/yourkit-java-profiler

For adoptopenjdk we can run the following command to see the available installable java versions:

$ brew search jdk
==> Casks
adoptopenjdk                               adoptopenjdk12-jre                         adoptopenjdk8-openj9
adoptopenjdk10                             adoptopenjdk12-openj9                      adoptopenjdk8-openj9-jre
adoptopenjdk11                             adoptopenjdk12-openj9-jre                  adoptopenjdk8-openj9-jre-large
adoptopenjdk11-jre                         adoptopenjdk12-openj9-jre-large            adoptopenjdk8-openj9-large
adoptopenjdk11-openj9                      adoptopenjdk12-openj9-large                adoptopenjdk9
adoptopenjdk11-openj9-jre                  adoptopenjdk8 ✔                            oracle-jdk
adoptopenjdk11-openj9-jre-large            adoptopenjdk8 ✔                            oracle-jdk-javadoc
adoptopenjdk11-openj9-large                adoptopenjdk8 ✔                            sapmachine-jdk
adoptopenjdk12                             adoptopenjdk8-jre

Step2: Install the desired java version

If we want to install a specific version of JDK, we can easily do that using the following command:

$ brew cask install java11

If the java11 installation happens successfully then it will show the following status:

Updating Homebrew...
==> Satisfying dependencies
==> Downloading https://download.oracle.com/java/GA/jdk11/9/GPL/openjdk-11.0.2_osx-x64_bin.tar.gz
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'java11'.
==> Installing Cask java11
==> Moving Generic Artifact 'jdk-11.0.2.jdk' to '/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk'.
Password:
?  java11 was successfully installed!

We can confirm this by checking the installed java version using the following command:

$ java -version
openjdk 11.0.2 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

Show all java versions installed on Mac:

$ /usr/libexec/java_home -V

Matching Java Virtual Machines (2):
    11.0.2, x86_64:	"OpenJDK 11.0.2"	/Library/Java/JavaVirtualMachines/openjdk-11.0.2.jdk/Contents/Home
    1.8.0_222, x86_64:	"AdoptOpenJDK 8"	/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Want to download Java?
If you wish to download Java on Mac
Java standard edition download

Want to install Maven on Mac?
Read about maven installation on Mac