In this tutorial, we will discuss how to install OpenJDK 14 on Mac systems. It provides the steps required to install OpenJDK 14 on macOS Catalina. The steps should be the same for other versions of macOS including Sierra, High Sierra, and Mojave.
Notes: Oracle Java updates and new releases available on its official site are not free since 16th April 2019. The consumer has to purchase a commercial license unless and otherwise Oracle Java can be used for personal and development purposes. We can still use Java for personal, development, and commercial usage without purchasing any license by using the OpenJDK distributed by Oracle under the GNU General Public License, version 2, with the Classpath Exception, and available on http://jdk.java.net.
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 14
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 14 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 as shown below.
# Move to the download location cd ~/Downloads
# Check the downloaded file name using ls -la command
# Extract the download
tar -xf openjdk-14.0.1_osx-x64_bin.tar.gz
# Check the OpenJDK version using the list command
# Output of ls -la command on my system
total 394832
drwx------@ 6 alpha staff 192 Apr 27 02:09 .
drwxr-xr-x+ 17 alpha staff 544 Apr 26 21:34 ..
-rw-r--r--@ 1 alpha staff 6148 Apr 26 23:10 .DS_Store
-rw-r--r-- 1 alpha staff 0 Apr 22 05:17 .localized
drwxr-xr-x@ 3 alpha staff 96 Mar 5 03:06 jdk-14.0.1.jdk
-rw-r--r--@ 1 alpha staff 193293652 Apr 27 01:57 openjdk-14.0.1_osx-x64_bin.tar.gz
Optionally, remove the existing Oracle JDK 14. I have removed the existing Oracle JDK 14 (Fig 1 and Fig 4) to install OpenJDK 14. You may also rename the OpenJDK 14 to install it parallel to the existing Oracle JDK 14.
# Uninstall existing Oracle JDK 14 sudo rm -rf "/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk"
Install OpenJDK 14 extracted by us.
# Install the JDK sudo mv jdk-14.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 "14.0.1" 2020-04-14 OpenJDK Runtime Environment (build 14.0.1+7) OpenJDK 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)
You might be required to switch the active JDK in case it does not reflect the OpenJDK 14. 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 (4): 14.0.1, x86_64: "OpenJDK 14.0.1" /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home 13.0.2, x86_64: "Java SE 13.0.2" /Library/Java/JavaVirtualMachines/jdk-13.0.2.jdk/Contents/Home 11.0.7, x86_64: "Java SE 11.0.7" /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home 1.8.0_251, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home
It should list the OpenJDK 14 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 14 on Mac and also provided the steps required to compile and run the first Java program using a simple Hello World program.