In this tutorial, we will discuss how to install the open-source version of Java i.e. OpenJDK 15 on Mac systems. It provides the steps required to install OpenJDK 15 on macOS Catalina. The steps should be the same for other versions of macOS including Sierra, High Sierra, and Mojave.
Notes: As an alternative to the commercial releases of Java i.e. Oracle Java, we can use OpenJDK for personal, development, and commercial usage without purchasing any license. Also, OpenJDK is distributed by Oracle under the GNU General Public License, version 2, with the Classpath Exception, and available on http://jdk.java.net.
The newer version of this tutorial is available at How To Install OpenJDK 17 On Mac.
System Checks
In this step, we will test whether Java is already installed or not. To do so, open the terminal and type java -version as shown in Fig 1. It shows that Java is already installed on my system.
It might show the message - "No Java runtime present, requesting install" in absence of Java and also opens a dialog to know more about it as shown in Fig 2.
Click the OK Button to hide the dialog in case it's displayed on your system in absence of Java.
Download OpenJDK 15
Open the download link to select the available versions as shown in Fig 3.
Click the Download Link as highlighted in Fig 3 to download OpenJDK 15 for macOS.
Install JDK
The default location used by macOS to search for the available JDK is - /Library/Java/JavaVirtualMachines. We can use the below-mentioned command to find the available JDKs installed on the system.
# List installed JDKs /usr/libexec/java_home -V
It will list down the available JDKs as shown in Fig 4.
Now, install the OpenJDK downloaded in the previous step using the commands shown below.
# Move to the download location cd ~/Downloads
# Check the downloaded file name using ls -la command # Extract the download tar -xf openjdk-15.0.1_osx-x64_bin.tar.gz
# Check the OpenJDK version using the list command # Output of ls -la command on my system total 393680 drwx------@ 6 bravo staff 192 Oct 21 23:59 . drwxr-xr-x+ 15 bravo staff 480 Oct 21 07:03 .. -rw-r--r--@ 1 bravo staff 6148 Oct 21 23:59 .DS_Store -rw-r--r-- 1 bravo staff 0 Oct 20 23:17 .localized drwxr-xr-x@ 3 bravo staff 96 Sep 15 09:39 jdk-15.0.1.jdk -rw-r--r--@ 1 bravo staff 192652449 Oct 21 23:56 openjdk-15.0.1_osx-x64_bin.tar.gz
Optionally, remove the existing Oracle JDK 15. I have removed the existing Oracle JDK 15 (Fig 1 and Fig 4) to install OpenJDK 15. You may also rename the OpenJDK 15 to install it parallel to the existing Oracle JDK 15.
# Uninstall existing Oracle JDK 15 sudo rm -rf "/Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk"
Install OpenJDK 15 extracted by us.
# Install the JDK sudo mv jdk-15.0.1.jdk /Library/Java/JavaVirtualMachines/
Now we will verify the installation as shown below.
# Check Java version java -version
# It should show the version installed by us openjdk version "15.0.1" 2020-10-20 OpenJDK Runtime Environment (build 15.0.1+9-18) OpenJDK 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)
You might be required to switch the active JDK in case it does not reflect the OpenJDK 15. Again check the list of installed JDKs as shown below.
# List installed JDKs /usr/libexec/java_home -V
# It will output the list of the available JDKs Matching Java Virtual Machines (1): 15.0.1, x86_64: "OpenJDK 15.0.1" /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home
It should list the OpenJDK 15 installed by us. This is how we can install and verify Java on macOS.
Switch JDK
We can switch between the multiple JDKs using the command as shown below.
# List installed JDKs /usr/libexec/java_home -V
# Switch active JDK export JAVA_HOME=`/usr/libexec/java_home -v <version>`
# Example export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_251`
This switches the Java version for the active shell. If you close the terminal and again check the Java version by opening a new terminal, your changes won't be reflected. You can follow How To Switch Java Version On Mac to system-wide update the Java version.
Getting started with Java - Hello World
In this step, we will write, compile, and execute our first program in Java using the standard Hello World example.
Now write the first Java program as shown below, save the program as HelloWorld.java and exit the editor. Make sure that the class name and file name are the same.
class HelloWorld {
public static void main( String[] args ) {
System.out.println( "Hello World !!" ); } }
Now open the command prompt and navigate to the path where you have saved your Java program. Use the below-mentioned commands to compile and execute the program.
# Compile - Specify file name and extension javac HelloWorld.java
# Execute - Spicy file name
java HelloWorld
# Output Hello World !!
These are the easy-to-install steps required to install Oracle JDK on Mac and write, compile and execute the first Java program.
Summary
This tutorial provided all the steps required to install Oracle JDK 15 on Mac and also provided the steps required to compile and run the first Java program using a simple Hello World program.