Explains the steps required to install OpenJDK 12 on Mac and getting started with Java programming using Hello World.

How To Install OpenJDK 12 On macOS
How To Install OpenJDK 12 On macOS
September 14, 2019

This tutorial provides all the steps required to install Java 12 on macOS. The steps should be the same for other versions of macOS.

Important Notes: Oracle has already announced that the Java updates and new releases available on its official site won't be free from 16th April 2019 onwards. The consumer has to purchase a commercial license unless and otherwise Java is used for personal and development purpose. 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.

This tutorial covers the installation of OpenJDK on macOS Sierra. The same steps can be followed for macOS Mojave and macOS Catalina.

Notes: JDK 13 is about to release on 17th Sept 2019. You may follow What's New In Java 13 to know about the new features introduced in JDK 13.

Download JDK

Open the JDK 12 GA Release site and download the distribution for macOS systems as shown in Fig 1.

OpenJDK Download

Fig 1

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 2. In my case, none of the JDK was installed while writing this tutorial.

Installed JDKs

Fig 2

Now, install the OpenJDK downloaded in the previous step using the commands as shown below.

# Move to the download location
cd ~/Downloads

# Extract the download
tar -xf

# Install the JDK
sudo mv jdk-12.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 "12.0.2" 2019-07-16 OpenJDK Runtime Environment (build 12.0.2+10) OpenJDK 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)/pre>

We can also check the list of installed JVMs as shown below

# List installed JDKs
/usr/libexec/java_home -V

# It will list the available JDKs
Matching Java Virtual Machines (1): 12.0.2, x86_64: "OpenJDK 12.0.2" /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home/pre>

This is how we can install and verify Java on macOS.

Switch JDK

You can switch between the multiple JDKs using the command as shown below.

# List installed JDKs
/usr/libexec/java_home -V

# Switch active JDK
/usr/libexec/java_home -v <version>

# Example
/usr/libexec/java_home -v 1.8.0_201

Getting started with Java - Hello World

In this section, we will write our first program in Java using the nano editor.

>sudo mkdir /programs/java
>cd /programs/java
>nano HelloJava.java

Now write the first Java program as shown in Fig 3, save the program and exit the editor.

OpenJDK HelloWorld

Fig 3

Use the javac and java to compile and execute the program as shown below.

// Compile the program
javac HelloWorld.java

// Execute the program
java HelloWorld

// Program output
Hello World

These are the basic steps to write, compile, and execute Java programs.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS