How to Install Java on CentOS 7

If you want to install Java on a VPS running CentOS 7 then this is the right tutorial for you. You will also be able to learn how to set a Java home variable and overview some basic management steps to get you started. As we already know Java is at the top of the list of the programming languages and this is worldwide. This programming language is created to have very little dependencies and that allows application developers to “write once, run everywhere”. As a matter of fact, any platform that supports Java can be operated by a compiled Java code. It can also be used to build both simple web applications and advanced software.

OpenJDK and Oracle Java are the two different implementations of Java. These two implementations of Java are based on the same code, but the main difference is that the reference implementation of Java or Open JDK is entirely open source, while on the other hand, Oracle Java contains some proprietary code.

With all that being said let`s go through all of the steps for installing Java on CenOS .

Install OpenJDK 8

Depending on what you re going to use Java for, you can choose JRE or JDK. For example, you should choose JRE only if you don`t plan to write software. But, if you plan to write or edit programs you should definitely choose JDK.

Our recommendation is to use JDK. It can be installed with the following command:

sudo yum install java-1.8.0-openjdk-devel

Don`t forget to verify the completion after the installation is finished. The output should be:

# yum list installed | grep "java"
java-1.8.0-openjdk.x86_64 1:1.8.0.171-8.b10.el7_5 @updates
java-1.8.0-openjdk-devel.x86_64 1:1.8.0.171-8.b10.el7_5 @updates
java-1.8.0-openjdk-headless.x86_64 1:1.8.0.171-8.b10.el7_5 @updates
javapackages-tools.noarch 3.4.1-11.el7 @base
python-javapackages.noarch 3.4.1-11.el7 @base
tzdata-java.noarch 2018e-3.el7 @updates

Install Oracle Java 8 JDK

Java RPM packages and the complied source code are provided by the Oracle team. You should install Java using the complied source code, because from my own experience I can tell you that if you try to install Java using rpm packages you can face some issues. Once I have decided to use the compiled source code I have installed Java many times without any issues. You can download the latest Java SE Development Kit 8 release from its official download page or use the following wget command.

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http:%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u172-b11/a58eab1ec242421181065cdc37240b08/jdk-8u172-linux-x64.rpm"

The next thing on the list is the installation with this rpm command:

rpm -ivh jdk-8u172-linux-x64.rpm

Now you need to check the Java JDK installation.

java -version

If everything is all right with the installation, you should see the following output:

java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

Set a Default Java Version

You should always set one Java version as your default (i.e. the one that will run when a user runs the java command), if you have decided to install multiple versions. In addition, some application may require certain environment variables to be set to locate which Java installation you should use. Here you can find out how to do that.

You can use the alternatives command, which manages default commands through symbolic links if you want to select the default Java command.

In order to print the programs that provide the java command which are managed by alternatives, use this command:

sudo alternatives --config java

This is one example of the output:

There is 3 program that provides 'java'.

  Selection    Command
-----------------------------------------------
 + 1           /usr/java/jdk1.8.0_172/jre/bin/java
*  2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre/bin/java)
   3           /usr/java/jdk1.8.0_172-amd64/jre/bin/java

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

All you have to do is to enter the selection number to choose which java executable should be used by default.

Set Up Java Environment Variables

The environment variables are used so the applications which are based on Java can work. You can set them using these commands below:

export JAVA_HOME=/usr/java/jdk1.8.0_172/
export PATH=$PATH:$JAVA_HOME

At this point, if you want to auto set environment variables on the system boot you should add the above commands to /etc/bashrc file.

Now, let us check for the environment variables using commands:

echo $JAVA_HOME

Sample output:

/usr/java/jdk1.8.0_172/

Or

echo $PATH

Sample output:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin:/usr/java/jdk1.8.0_172/

That is it. You have successfully installed and configured Java on your CentOS 7 server. Of course, you don’t have to install Java on CentOS 7 if you use one of our Linux Cloud VPS Hosting plans, in which case you can simply ask our expert Linux admins to how to install JAVA on CentOS 7 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment