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.
The latest version is Java 17 which was released in September 2020. This tutorial helps you to install Java on the macOS system.
Step 1 – Prerequisites
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.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
For more instruction visit Homebrew installation tutorial.
Step 2 – Install JAVA on macOS
At the time of writing this tutorial the available version, Java 11 LTS and Java 15 latest for the installation.
- Tap adoptopenjdk/openjdk- Use the following commands to view the details about java versions to be install.
brew tap adoptopenjdk/openjdk
- Search Available JDK- View all the available Java versions to install
brew search jdk
- Install Java – Use one of the following commands to install Java 8, 11, and 15. Generally, we required Java 8, 11 LTS versions.
brew install --cask adoptopenjdk8
##Install JDK 8
brew install --cask adoptopenjdk11
##Install JDK 11
brew install --cask adoptopenjdk15
##Install JDK 15
The installation process may take some time to complete depending on your network speed.
- Check Version – Once the installation finished, verify the installed Java version.
java -versionopenjdk version "15.0.1" 2020-10-20 OpenJDK Runtime Environment AdoptOpenJDK (build 15.0.1+9) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 15.0.1+9, mixed mode, sharing)
Default latest installed version will be active on your machine.
Step 3 – Switching Between Java Version’s
You system may have installed multiple Java versions. For example, I have installed Java 8, 11 and 15 on my system. To view the installed Java version execute:
/usr/libexec/java_home -VMatching Java Virtual Machines (3): 15.0.1, x86_64: "AdoptOpenJDK 15" /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home 126.96.36.199, x86_64: "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home 1.8.0_275, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home
Create or edit the existing ~/.bash_profile in your favorite text editor. Then export the JAVA_HOME environment variable to a specific java version, you want to use as default on Mac.
export JAVA_8=$(/usr/libexec/java_home -v1.8) export JAVA_11=$(/usr/libexec/java_home -v11) export JAVA_15=$(/usr/libexec/java_home -v15) # Java 8 #export JAVA_HOME=$JAVA_8 # Java 11 export JAVA_HOME=$JAVA_11 # Java 15 # export JAVA_HOME=$JAVA_15
Save the file and close it. The above settings will make JAVA_HOME point to JAVA 11.
Execute the following command to reload the environment.
All done, check the current active java version
java -versionopenjdk version "188.8.131.52" 2020-11-04 OpenJDK Runtime Environment AdoptOpenJDK (build 184.108.40.206+1) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 220.127.116.11+1, mixed mode)
When you want to switch the JDK versions again, update the ~/.bash_profile and export JAVA_HOME to another JDK version.
This tutorial explained you to how to install Java on macOS. Also provides you steps to switching between multiple installed Java versions.
You can visit Github page of the OpenJDK casks.