Install Java on Mac

Prerequisite

Make sure you have Homebrew installed on your Mac OS. In order to check whether Homebrew or brew installed or not, simply using following command. It helps you to check the brew version also.

$ brew --version
Homebrew 2.1.6
Homebrew/homebrew-core (git revision fccd8; last commit 2019-07-04)
Homebrew/homebrew-cask (git revision e06c2; last commit 2019-07-04)

Install Latest JDK

Since OpenJDK is not a part of Homebrew default repository and it is available in brew cask you need install it by having cask in your brew command like this:

$ brew cask install java

The log for jdk 12 that I installed looked like something like this

 ==> Satisfying dependencies
 ==> Downloading https://download.oracle.com/java/GA/jdk12.0.1/69cfe15208a647278a19ef0990eea691/12/GPL/openjdk-12.0.1_osx-x64_bin.tar.gz
 ################################################################## 100.0%
 ==> Verifying SHA-256 checksum for Cask 'java'.
 ==> Installing Cask java
 ==> Moving Generic Artifact 'jdk-12.0.1.jdk' to '/Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk'.

You can check the version of java using following command

$ java -version
openjdk version "12.0.1" 2019-04-16
OpenJDK Runtime Environment (build 12.0.1+12)
OpenJDK 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

Install Older Versions of JDK

If you want to install the older versions of java using following brew command you will update the caskroom taps

$ brew tap caskroom/versions
Updating Homebrew…
==> Auto-updated Homebrew!
 Updated 5 taps (homebrew/cask-versions, homebrew/core, homebrew/cask, caskroom/versions and caskroom/cask).
==> Updated Formulae
 babel                                                    freetype                                                 ucloud

Using following command you can search available open jdk versions

$ brew search java
==> Formulae
 app-engine-java                   google-java-format                javarepl                          jslint4java                       libreadline-java
==> Casks
 charles-applejava           eclipse-javascript          java-beta                   java6                       netbeans-java-se            yourkit-java-profiler
 charles-applejava           eclipse-javascript          java-beta                   java6                       netbeans-java-se            yourkit-java-profiler
 eclipse-java                java ✔                      java11                      netbeans-java-ee            oracle-jdk-javadoc
 eclipse-java                java ✔                      java11                      netbeans-java-ee            oracle-jdk-javadoc

You can use following command to install and older version of jdk such as 11

$ brew cask install java11

If you get following error

Error: Cask java11 exists in multiple taps:
   homebrew/cask-versions/java11
   caskroom/versions/java11

Simply use either one of the paths give above homebrew/cask-versions/java11 or caskroom/versions/java11

$ brew cask install caskroom/versions/java11
==> 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!

Use jEnv to Switch JDKs

You can refer to Switch JDKs using jEnv page, if you have multiple JDKs installed on your machine, and you need to switch between them fast and without going and set $JAVE_HOME and $PATH environment variables

Leave a Reply