How To Install Java On CentOS 7?

Introduction

Java is a popular programming software that allows developers to create and run Java applets and applications. The software solution has three editions:

  • The Java Standard Edition (SE)
  • The Java Enterprise Edition (EE)
  • The Java Micro Edition)

Our tutorial will focus on the first edition (SE), which comes in two different packages: Java Development Kit (JDK) and Java Runtime Environment (JRE). The Runtime Environment (JRE) is deployed on a Java Virtual Machine and is used by developers to run applets and applications compiled in Java programming language. On the other hand, the Java Development Kit (JDK) is used to write, develop, and compile applets and applications.

Furthermore, there is two implementations of Java: The Oracle Java and OpenJDK. These implementations are built on a similar code, but the OpenJDK is completely open-source while the Oracle Java has a proprietary code. However, most applications compiled in Java works seamlessly with both implementations.

You only need a single installation of Java, but it’s also easy to implement a number of versions and releases on one system. This tutorial will help you install various version and releases of Java implementation. Feel free to go for the version or release that suits your style of working:

Step 1 - Installing OpenJDK JDK

This section is compiled to help you install OpenJDK JDK using yum.

Installing OpenJDK8 JDK

Run the command below:

$ sudo yum installjava-1.8.0-openjdk-devel

Type Y and select RETURN. That’s it! The installation is complete.

Installing OpenJDK7 JDK

To complete this installation, run the command below:

$ sudo yum installjava-1.7.0-openjdk-devel

Type Y on confirmation request, and select RETURN to proceed with Java installation. That’s it! The installation is successful.

Installing OpenJDK6 JDK

To implement OpenJDK6 JDK, run the command below:

$ sudo yum installjava-1.6.0-openjdk-devel

Type Y on confirmation request, and select RETURN to proceed with Java installation. That’s it! Your installation is successful.

Step 2 - Installing OpenJDK JRE

This section is compiled to help you install OpenJDK 8 JRE using yum.

Installing OpenJDK8 JRE

Execute the command below:

$ sudo yum installjava-1.8.0-openjdk

You will be presented with a confirmation prompt, type y, then select RETURN to proceed with Java installation.

Installing OpenJDK7 JRE

To implement OpenJDK7 JRE, execute the command below:

$ sudo yum installjava-1.7.0-openjdk

Type Y on confirmation request, and select RETURN to proceed with Java installation. That’s it! You installation is complete.

Installing OpenJDK6 JRE

Run the command below to deploy OpenJDK6 JRE:

$ sudo yum installjava-1.6.0-openjdk

Type Y on confirmation request and select RETURN to proceed with Java installation.

Step 3 - Installing And Configuring Oracle Java JRE

This part will help you install various versions of Oracle Java JRE. We’ll use the wget command to get all the required Oracle Java software modules. But the first thing is installing the wget since its not included in the default CentOS 7 repository.

Run the following command to accomplish this:

$ sudo yum install wget

Installing The Oracle Java9 JRE

To implement Oracle Java9 JRE, first change to the home directory, then download the required RPM files for Oracle Java9 JRE:

$ cd ~
$ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie""http://link_copied_from_site"

Next, use the yum command to install the RPM:

$ sudo yum localinstall jre-9.0.4_linux_x64_bin.rpm

Type Y on confirmation request and select RETURN to proceed with Java installation. Java will be installed in the directory /usr/java/jre-9.0.4/bin/java. It will also be linked from the location /usr/bin/java.

Now, you can do away with the archive files downloaded earlier

$ rm ~/jre-9.0.4_linux_x64_bin.rpm

That’s it! You have successfully deployed Oracle Java9 JRE.

Installing The Oracle Java8 JRE

This is the same as installation version 9. First, run the command below to switch to the home directory, then download the required RPM:

$ cd ~
$ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie""http://link_copied_from_site"

Next, run the command below to install the RPM:

$ sudo yum localinstall jre-8u161-linux-x64.rpm

Type Y at confirmation request and select RETURN to proceed with Java installation. Java will be deployed at /usr/java/jre1.8.0_161/bin/java, then linked from the directory /usr/bin/java.

Now, execute the command below to trash the archive files downloaded earlier:

$ rm ~/jre-8u161-linux-x64.rpm

That’s it! Oracle Java8 JRE is successfully installed.

Step 4 - Installing The Oracle Java JDK

Installing The Oracle Java9 JDK

Run the command below to change to the home directory, then download the required RPM:

$ cd ~
$ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie""http://link_copied_from_site"

Then, execute the command below to install RPM:

$ sudo yum localinstall jdk-9.0.4_linux-x64_bin.rpm

Type Y when prompted for confirmation and select RETURN  to proceed with Java installation. Java will be implemented at /usr/java/jdk-9.0.4/bin/java. The installation will be linked from /usr/bin/java.

Now, you do not need the archive files, run the command below to delete them:

$ rm ~/jdk-9.0.4_linux-x64_bin.rpm

That’s it! Oracle Java9 JDK is successfully installed.

Installing The Oracle Java8 JDK

Execute the commands below to switch to the home directory, thendownload the required RPM archive files:

$ cd ~
$ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie""http://link_copied_from_site"

Next, run the command below to install RPM:

$ sudo yum localinstall jdk-8u161-linux-x64.rpm

Type Y at confirmation request and select RETURN to proceed with Java installation: by now, Java is implemented at /usr/java/jdk1.8.0_161/jre/bin/java. It also linked from /usr/bin/java.

That’s it! Oracle Java8 JDK is successfully installed.

Step 5 - Setting The Default Java

This is a vital step especially if you have implemented numerous Java versions. In this section, we’ll help you set one of the implementations as the default Java version.

First, execute the command below to check the Java versions set as the default implementation:

$ java -version

Using Alternative

The alternative command is vital since it helps you manage all default commands via symlinks. Its a popular option used to choose the default command for Java:

$ sudo alternatives --config java

This will give you an output similar to:

There are 5 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           java-1.7.0-openjdk.x86_64(/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.161-2.6.12.0.el7_4.x86_64/jre/bin/java)
   2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-5.b12.el7_4.x86_64/jre/bin/java)
   3           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
*+ 4           /usr/java/jre-9.0.4/bin/java
   5           /usr/java/jdk-9.0.4/bin/java

Enter to keep the current selection[+], or type selection number:

Type the number of your preferred option to set the default Java executable.

Utilizing Environment Variables

Many applications compiled in Java utilize JRE_HOME or JAVA_HOME variables to select the Java executable to utilize.

Let’s say you implemented Java to /usr/java/jdk1.8.0_161/jre/bin, then you can simply set the  JAVA_HOME variable in the bash shell, using:

$ export JAVA_HOME=/usr/java/jdk1.8.0_161/jre

Alternatively, run the command below to set JAVA_HOME as the default Java executable for each user on your system:

$ sudo sh -c "echo export JAVA_HOME=/usr/java/jdk1.8.0_161/jre >> /etc/environment"

That is it! You have successfully installed Java on your CentOS system.

Check out these top 3 Java hosting services:

Was this article helpful?