Java version on MacOS

To view installed java versions on Mac use this command

/usr/libexec/java_home -V

The output will be like (abridged):

Matching Java Virtual Machines (9):
11.0.12, x86_64: "Zulu 11.50.19" /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home
1.8.0_302, x86_64: "Zulu 8.56.0.21" /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
1.8.0_251, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

To pick the version to be the default, it needs to be set in the shell resource file (~/.zshrc or ~/.bash_profile):

export JAVA_HOME=`/usr/libexec/java_home -v 11.0.12`

or

export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.12)

Now, source the shell file and verify java version using these commands:

source ~/.zshrc
java -version

Now, if you are not happy with the existing version and need a different one:

brew search java

You will see the available formulae and casks:

==> Formulae
app-engine-java java ✔ java11 javarepl libreadline-java
google-java-format java-service-wrapper javacc jslint4java pdftk-java
==> Casks
charles-applejava java6 eclipse-javascript
java-beta eclipse-java oracle-jdk-javadoc

If you are interested in any, for example java11, run:

brew info java11

Output will be something like this:

openjdk@11: stable 11.0.12 (bottled) [keg-only]
Development kit for the Java programming language
https://openjdk.java.net/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjdk@11.rb
License: GPL-2.0-only
==> Dependencies
Build: autoconf ✘
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
==> Analytics
install: 35,691 (30 days), 120,830 (90 days), 470,337 (365 days)
install-on-request: 17,188 (30 days), 56,829 (90 days), 205,326 (365 days)
build-error: 445 (30 days)

The first word is your key. Now you can get it by running:

brew install openjdk@11

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store