CentOS

How to Install JDK 12 on CentOS 7

Java Development Kit (JDK) is used to develop and test Java applications. It is used by millions of Java developers around the world.

JDK has 2 versions. OpenJDK and Oracle JDK. OpenJDK is an open source implementation of Oracle JDK. Oracle JDK contains some proprietary codes that OpenJDK does not have. OpenJDK is open source where Oracle JDK is not.

In this article, I am going to show you how to install Oracle JDK 12 and OpenJDK 12 on CentOS 7. So, let’s get started.

Installing Oracle JDK 12:

Oracle JDK 12 is not available in the official package repository of CentOS 7. But, you can easily download Oracle JDK 12 from the official website of Oracle and install it on CentOS 7.

First, visit https://www.oracle.com/technetwork/java/javase/downloads/index.html from your favorite browser. Once the page loads, click on the DOWNLOAD Java Platform (JDK) 12 button as marked in the screenshot below.

Now, click on Accept License Agreement to accept the Oracle Technology Network License Agreement for Oracle Java SE.

Your browser should prompt you to save the file. Select Save File and click on OK.

jdk 12.0.1 RPM package is being downloaded.

Once the download is complete, navigate to the ~/Downloads directory as follows:

$ cd ~/Downloads

As you can see, the RPM package file I just downloaded is here.

Now, install the RPM package file with the following command:

$ sudo rpm -Uvh jdk-12.0.1_linux-x64_bin.rpm

Oracle JDK 12 is being installed.

Oracle JDK 12 should be installed at this point.

Now, to check that the installation was successful and Oracle JDK 12 is working, run the following command:

$ java -version

As you can see, Oracle JDK 12 is working.

Installing OpenJDK 12:

OpenJDK 12 is not available in the official package repository of CentOS 7. But, you can easily download it from the official website of OpenJDK and install it on CentOS 7.

To download OpenJDK 12, visit https://jdk.java.net/12/ from your favorite web browser and click on the .tar.gz link as marked in the screenshot below.

Your browser should prompt you to save the OpenJDK 12 .tar.gz archive. Select Save File and click on OK.

Your browser should start downloading OpenJDK 12 archive. It may take a while to complete.

Once the download is complete, navigate to the ~/Downloads directory with the following command:

$ cd ~/Downloads

As you can see, the OpenJDK 12 archive you’ve just downloaded is here.

Now, extract the OpenJDK 12 archive in the /opt directory with the following command:

$ sudo tar xvzf openjdk-12.0.1_linux-x64_bin.tar.gz -C /opt

The OpenJDK 12 archive should be downloaded.

As you can see, a new directory is created in the /opt directory. Note the directory name.

Now, you have to add the JAVA_HOME environment variable and add the OpenJDK 12 binary directory to the PATH environment variable. To do that, run the following command:

$ echo -e 'export JAVA_HOME="/opt/jdk-12.0.1"\nexport PATH="$PATH:${JAVA_HOME}/bin"\n' |
 sudo tee /etc/profile.d/openjdk12.sh

A new file /etc/profile.d/openjdk12.sh should be created.

Now, reboot your computer with the following command:

$ sudo reboot

Once your computer boots, run the following command to check whether the environment variables JAVA_HOME and PATH is set correctly.

$ echo $JAVA_HOME && echo $PATH

As you can see, the environment variables are correctly set.

Now, check whether OpenJDK 12 is working correctly with the following command:

$ java -version

OpenJDK version 1.8.0_212! This is not the version we installed.

The problem is another version of OpenJDK is installed on your CentOS 7 machine. To solve this problem, just remove the old OpenJDK packages from your CentOS 7 machine as follows:

$ sudo yum remove $(sudo rpm -qa | grep openjdk)

Now, press y and then press <Enter> to continue.

The older version of OpenJDK should be removed.

As you can see, now the version is correctly displayed. So, the OpenJDK we’ve installed earlier is being used.

Compiling and Running a Simple Java Program with JDK 12:

In this section, I am going to compile and run a simple Java program with JDK 12.

I wrote a simple Java program in a file Welcome.java and put it in the ~/codes directory on my login users HOME directory.

The contents of the Welcome.java Java source file are:

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to LinuxHint!");

}

}

First, navigate to the directory where the Welcome.java source file is.

$ cd ~/codes

Then, to compile the Welcome.java Java source file, run the following command:

$ javac Welcome.java

Once the Welcome.java source file is compiled, a Welcome.class file should be generated. This is a Java class file. It contains Java bytecodes that the Java Virtual Machine (JVM) can understand.

As you can see, the correct output is displayed.

So, that’s how you install Oracle JDK 12 and OpenJDK 12 on CentOS 7. Thanks for reading this article.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.