Java is one of the most used programming languages for backend and creating different type of applications ( web application, mobile applications, desktop applications, and many more). It supports all the major operating systems such as Windows, Linux, Solaris, and MacOS. In this topic, we will see how to install Java on mac and its different variations.
Before proceeding further, let’s have a look at the quick index of the topic:
- System Requirements for Installing the JDK and JRE on macOS
- Determining the JRE Version Installed on macOS
- Determining the Default JDK Version on macOS
- Installing the JDK on macOS
- Installing the JRE on macOS
- Installing By Homebrew
- Version Switching
- Uninstalling the JDK on macOS
- Uninstalling the JRE on macOS
System Requirements to install Java on mac
The system requirements and considerations for installing the JDK and the JRE on macOS are as following:
Important points to be considered while installing Java on MacOS:
A computer running macOS and having administrator privileges.
Java can not be installed for a single user instead it is installed systemwide for all users.
The JDK package contains JRE by default. However, the current JRE version will not be replaced with the lower version of JRE.
Installing the JRE will not contain the JVM or other Java libraries for creating applications. It can only be used to run the Java apllications. See the difference between JRE, JDK, and OracleJDK.
The following requirements should be meet to install Java 7 or later versions:
- Intel-based Mac running versions 10.7.3 and above
- Administrator privileges
- A 64-bit browser (e.g., Safari)
- 32-bit browsers do not support Java 7 and later versions on the Mac platform
Determining the JRE Version Installed on macOS
There can be only one JRE version on the system. Installing the JRE will replaces the previous version. However, it will not replace with the older versions.
The JRE version can be determned by the following ways:
Visit System Preferences->Other section->Java . This will start the Java Control Panel, after this, click on About to display the version information.
Enter the following command on terminal:
% /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version
The above command will display the installed Java version.
Determining the Default JDK Version on macOS
To start a Java application, the system uses the default JDK version. It is possible JRE may have the a different version that the JDK.
There can be multiple versions of the JDK, but, the JRE version can be only one (latest).
To determine the installed Java version, execute the below command:
The above command will display the output something like this ( for Java 10):
java version "10.0.2.1-ea" Java(TM) SE Runtime Environment (build 10.0.2.1-ea+13) Java HotSpot(TM) 64-Bit Server VM (build 23.2-b04, mixed mode)
To change the default version version of Java, either specify the full path, or use the java_home tool as follows:
/usr/libexec/java_home -v 10.0.2.1 --exec javac -version
Installing the JDK on macOS
The JDK package by default contains the associated JRE versions. The JavaFX SDK and Runtime are also packaged into the standard JDK package.
Follow the below steps to install Java on MacOS:
Download the JDK .dmg file from Here, or by executing the below command:
before downloading the file you must accept license agreements.
After the successfull Double click on the ‘.dmg’ file to start the installation process.
Next, you will see a finder window having the name of the ‘.pkg’ file. Click on the ‘.pkg’ file to proceed the installation process:
In this step, you will see a Introduction window, click Continue.
You will see a installation type window, Click Install to continue.
In this step you will see a window having a message “Installer is trying to install new software”. Type your administrator password to allow this.
Enter the Administrator user name and password and click Install Software.
The downlaoded Java version is installed on your machne and confirmation window is displayed.
Installing the JRE on macOS
Follow the below steps to install the JRE on an macOS platform:
Download the JRE .dmg file, by executing the below command:
Accept the license agreement.
Next, double-click on the .dmg file and launch it.
In this step, you will see a finder window appears having ‘.app’ file.
Double-click on the Java 10.app icon, it will start the installation process.
The installation process will display the Welcome window, Click Next to continue the process.
Hence, the downloaded JRE version is installed and a confirmation window is displayed. Click Close to complete the process.
Installing By Homebrew
To install By Homebrew package manager, follow the following steps:
Step1: Install Homebrew and update it, and download openJDK package by executing the below commands:
brew update Add adoptopenjdk/openjdk. brew tap adoptopenjdk/openjdk
Step2: Find all available JDK versions by executing the below command:
brew search jdk
The output will be something like this:
==> Casks adoptopenjdk adoptopenjdk12 adoptopenjdk13-openj9 adoptopenjdk8-openj9-jre adoptopenjdk10 adoptopenjdk12-jre adoptopenjdk13-openj9-jre adoptopenjdk8-openj9-jre-large adoptopenjdk11 adoptopenjdk12-openj9 adoptopenjdk13-openj9-jre-large adoptopenjdk8-openj9-large adoptopenjdk11-jre adoptopenjdk12-openj9-jre adoptopenjdk13-openj9-large adoptopenjdk9 adoptopenjdk11-openj9 adoptopenjdk12-openj9-jre-large adoptopenjdk8 oracle-jdk adoptopenjdk11-openj9-jre adoptopenjdk12-openj9-large adoptopenjdk8 oracle-jdk-javadoc adoptopenjdk11-openj9-jre-large adoptopenjdk13 adoptopenjdk8-jre sapmachine-jdk adoptopenjdk11-openj9-large adoptopenjdk13-jre adoptopenjdk8-openj9
Step3: Select Java versions like Java 8, 9, 10, 11, 12, 13 by executing the one of the following commands:
brew cask install adoptopenjdk8 brew cask install adoptopenjdk9 brew cask install adoptopenjdk10 brew cask install adoptopenjdk11 brew cask install adoptopenjdk12 brew cask install adoptopenjdk13
Step4: for example to install Java 8 and 11, execute the below commads:
brew cask install adoptopenjdk8
brew cask install adoptopenjdk11
To display the loaction where Java is installed, execute the below command:
The above command will display the output something like this:
Matching Java Virtual Machines (2): 11.0.6, x86_64: "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home 1.8.0_242, x86_64: "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
Homebrew will install the JDK at Mac’s standard folder for Java “/Library/Java/JavaVirtualMachines/”.
Verify the installation, by executing the below command:
The output will be as:
openjdk version "11.0.6" 2020-01-14 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.6+10) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.6+10, mixed
By default, Mac takes the Higher version of Java.
To change the default version of Java create or edit the “~/.bash_profile” with the text editor of your choice like vim or nano, export JAVA_HOME variable to the specified JDK we want to use on Mac.
Execute the below command:
Update the file as:
export JAVA_HOME_8=$(/usr/libexec/java_home -v1.8) export JAVA_HOME_11=$(/usr/libexec/java_home -v11) export JAVA_HOME_14=$(/usr/libexec/java_home -v14) # Java 8 export JAVA_HOME=$JAVA_HOME_8 # Java 11 # export JAVA_HOME=$JAVA_HOME_11
The above script will make the Java 8 as default Java version.
Uninstalling the JDK on macOS
To uninstall the JDK on macOS, Navigate to “/Library/Java/JavaVirtualMachines”. Remove the directory whose name matches the following format:
For example, to uninstall Java 10, execute the below command:
rm -rf jdk-10.0.2.1.jdk
Note: Do not try to uninstall Java by removing the Java tools from /usr/bin. This directory is part of the system software and any changes will be reset by Apple the next time that you perform an update of the OS.
Uninstalling the JRE on macOS
To uninstall JRE, Navigate to “/Library/Internet Plug-Ins”. From here, remove the JavaAppletPlugin.plugin directory.
Now, navigate to “/Library/PreferencePanes”.