How-To VPS Hosting

How to Install Java on CentOS 8

Updated 4 min read

Java JDK is a free and open-source software development kit used for building applications, applets, and components using the Java programming language. Java JDK comes with a set of tools for developing and testing applications written in the Java programming language; these tools include JRE, Java, Javac (Compiler) and Jar (Archiver).

There are two different implementations of Java, OpenJDK and Oracle JDK:

  • OpenJDK is the free and open-source implementation of the Java Platform.
  • Oracle JDK is a paid and permits only non-commercial use of the software.

In this tutorial, we will show you how to install OpenJDK 8, OpenJDK 11 and Oracle Java JDK 14 on CentOS 8.

Step 1 – Install OpenJDK 11

By default, OpenJDK 11 is available in the CentOS 8 default repository. You can install it by running the following command:

dnf install java-11-openjdk -y

Once installed, verify the installed Java version with the following command:

java -version

You should see the following output:

Terminal output of java -version showing OpenJDK 11.0.6 installed

Step 2 – Install OpenJDK 8

By default, OpenJDK 8 is available in the CentOS 8 default repository. You can install it with the following command:

dnf install java-1.8.0-openjdk -y

Once installed, verify the installed Java version with the following command:

java -version

You should see the following output:

Terminal output of java -version showing OpenJDK 8 update 242 installed

Step 3 – Set Up the Default Java Version

At this point, you have two different versions of Java installed on your system. You can switch between multiple Java versions using the following command:

alternatives --config java

In the following output, you should see that OpenJDK 8 is the default version on your system.

Terminal output of alternatives --config java listing OpenJDK 8 and 11 with OpenJDK 8 selected

Type 1 and press Enter to switch the Java version from OpenJDK 8 to OpenJDK 11.

Now, verify the default Java version with the following command:

java -version

You should see that OpenJDK 11 is the default Java version on your system.

Terminal output of java -version confirming OpenJDK 11.0.6 is now the default version

Step 4 – Setup JAVA_HOME Environment Variable

In some Java-based applications, you will need to define the installation location of Java to specify which Java version you want to use to run the application. You can use the JAVA_HOME environment variable to define the Java installation path.

You can set the JAVA_HOME environment variable for OpenJDK 11 by editing the file /etc/profile:

nano /etc/profile

Add the following line at the end of the file:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.5.10-2.el8_1.x86_64/"

Save and close the file when you are finished. Then, activate the changes with the following command:

source /etc/profile

Next, verify the JAVA_HOME environment with the following command:

echo $JAVA_HOME

You should get the following output:

Terminal output of echo JAVA_HOME showing the OpenJDK 11 installation path

Step 5 –  Install Oracle Java 14

First, you will need to download the Oracle Java 14 RPM package file from the Oracle download page, as shown below.

Oracle Java SE Development Kit 14 downloads page with the Linux RPM package link highlighted

Here, you can see the different versions of Java 14. Click on jdk-14_linux-x64_bin.rpm  to download it in your system.

You may now need to upload the file to your Centos8 server from your local desktop. This can be done using WinSCP.

Download WinSCP and install WinSCP using the following guide:

guide install

Upload the file using your CentOS 8 server information:

WinSCP Login dialog with SFTP host, username and password fields for connecting to the server

WinSCP upload progress dialog transferring the JDK 14 RPM file to the server root directory

Terminal output of ll listing the uploaded JDK 14 Linux RPM file in the home directory

Once the upload has completed, you can install it with the following command:

dnf localinstall jdk-14_linux-x64_bin.rpm -y

Once installed successfully, change the default Java version to Oracle Java 8 with the following command:

alternatives --config java

You should see a list of different Java versions in the following output:

Terminal output of alternatives --config java listing OpenJDK 11, OpenJDK 8 and Oracle Java 14

Type 3 and hit Enter to change the default Java version.

Next, verify the Java default version with the following command:

java -version

You should see the following output:

Terminal output of java -version confirming Oracle Java 14 is now the default version

Conclusion

Congratulations! You have successfully installed Java on CentOS 8. I hope you have now enough knowledge to switch between different Java versions and start building your application on your virtual private server.

Share

Written by

View all articles →

Ready to Deploy?

Launch secure, compliant, enterprise-grade infrastructure with confidence.