Java is a general-purpose, secure, robust, object-oriented language developed by Sun Microsystems in 1990. Java is portable which means it follows to write once run anywhere paradigm. Many of the useful applications are built on Java and required a Java runtime environment. If you are preparing your system for developing new Java applications, you need to install JDK on your machine.
In this blog post, you will learn to install different-2 Java versions on macOS using Homebrew.
Pre-Requisites
Before starting the installation of Java using this tutorial you must have the following prerequisites
- Terminal: You must have Mac Terminal access and little knowledge about working with the terminal application. Ao login to your Mac system and open terminal
- Homebrew: This tutorial relies on Homebrew, So you must have homebrew installed. Homebrew can be installed with a single command. You can follow these instructions to install Homebrew
How to Install Java on MacOS
The Homebrew contains the latest stable java version along with the LTS release Java 8, 11 LTS, and Java 17. Follow the below steps to install the specific version of Java or all the available Java versions on your macOS system.
- Search Available JDK Version:
Open a terminal and use brew to search available versions of Java Formulae for your system.
brew search openjdk
==> Formulae openjdk ✔ openjdk@11 openjdk@17 openjdk@8 jd mdk cdkHere the formulae “openjdk” is always referred to as the latest Java version. Other than that, OpenJDK 8, 11, and 17 are also available.
- Install Latest Java Version
As you know that Brew’s “openjdk” formulae always refer to the latest version. You can install it with the following command.
brew install openjdk
Brew installation of OpenJDK doesn’t set reference than Celler directory. For the system, Java wrappers to find this JDK, symlink it with:
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
- Install Specific Java Version
Generally, the applications required some old or stable Java versions. So instead of installing default (latest) Java, install specific Java by its version number. Here Java 8, and 11 are the LTS versions.
Use one of the below options to install the required Java version. If required, You can also install all the versions on a single system:
- Install Java 8:
brew install openjdk@8
sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-1.8.jdk
- Install Java 11:
brew install openjdk@11
sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
- Install Java 17:
brew install openjdk@17
sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
The installation process may take some time to complete depending on your network speed.
- Install Java 8:
- Test Java Version
Once the installation finished, verify the installed Java version.
java -version
openjdk version "11.0.16.1" 2022-08-12 OpenJDK Runtime Environment Homebrew (build 11.0.16.1+0) OpenJDK 64-Bit Server VM Homebrew (build 11.0.16.1+0, mixed mode)
Setup JAVA_HOME and Switching Version
The /usr/libexec/java_home
command returns a path suitable for setting the JAVA_HOME environment variable. It determines this path from the preferred JVMs in the Java Preferences application. Use the below steps to configure JAVA_HOME environment variable. Also, know about switching to the default Java version.
- Use java_home command with
-V
option to list all the JVM configured on your macOS./usr/libexec/java_home -V
Matching Java Virtual Machines (4): 18.0.2.1 (x86_64) "Homebrew" - "OpenJDK 18.0.2.1" /usr/local/Cellar/openjdk/18.0.2.1/libexec/openjdk.jdk/Contents/Home 17.0.4.1 (x86_64) "Homebrew" - "OpenJDK 17.0.4.1" /usr/local/Cellar/openjdk@17/17.0.4.1/libexec/openjdk.jdk/Contents/Home 11.0.16.1 (x86_64) "Homebrew" - "OpenJDK 11.0.16.1" /usr/local/Cellar/openjdk@11/11.0.16.1/libexec/openjdk.jdk/Contents/Home 1.8.0_345 (x86_64) "Homebrew" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+345/libexec/openjdk.jdk/Contents/Home /usr/local/Cellar/openjdk/18.0.2.1/libexec/openjdk.jdk/Contents/Home - Starting with macOS Catalina, Zsh is used as the default login shell and interactive shell across the operating system. Check the default shell on your system with the following command:
echo $SHELL
/bin/zshThe above command shows that your system is using Zsh as the default shell. If the output shows
/bin/bash
then Bash will be the default shell. - The zsh shell users edit the ~/.zshenv (macOS 10.15 and newer) or bash shell users edit ~/.bash_profile in your favorite text editor.1234567## Uncomment one to make it default#export JAVA_HOME=$(/usr/libexec/java_home -v1.8)export JAVA_HOME=$(/usr/libexec/java_home -v11)#export JAVA_HOME=$(/usr/libexec/java_home -v17)#export JAVA_HOME=$(/usr/libexec/java_home -v18)export PATH="$JAVA_HOME/bin:$PATH"
Save the file and close it. The above configuration will set the JAVA_HOME environment variable and also update the PATH as well. Uncomment the line to change different Java versions:
- Reopen the shell to apply changes to execute the below command to load the environment in the current shell.
source ~/.zshenv
- All done, check the current active java version
java -version
openjdk version "11.0.9.1" 2020-11-04 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9.1+1) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9.1+1, mixed mode)
When you want to switch the JDK versions again, update the ~/zshenv
or ~/.bash_profile
and update JAVA_HOME to another JDK version.
Conclusion
This tutorial explained you to how to install Java on macOS. Also provides you steps to switching between multiple installed Java versions.
14 Comments
The brew folks have decided to break all the scripts and blogs that describe this process:
Error: Calling brew cask install is disabled! Use brew install [–cask] instead.
caskroom has moved to homebrew.
So: brew tap homebrew/cask
If anyone else is having problems with brew tap caskroom/cask, don’t brew tap just use brew cask and install java with `brew cask install java`.
For macos mojave
brew tap homebrew/cask-versions
brew cask info java11
brew cask install java11
And still `brew cask install java` for java 13
how do you install java12 now that java 13 will be installed when using “java”?
Thanks, Ruth, The article has been updated with latest versions.
Just go to https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html
with an Oracle account….
perfect, ty
Worked perfectly on my MAC. Thank you!!
Worked Perfectly for my on my MAC. Thank you for this.
Thanks
Perfecto! Thankyousir.
Thanks a lot… Got Java installed successfully on my Mac