How to Install JAVA 8 on CentOS/RHEL 7/6

This post is also available in: Türkçe

This tutorial helps you to install Java 8 or update Java on your system. Read the instruction carefully before downloading Java from the Linux command line.

The Oracle Java 11 is the latest LTS version available to download and install. To install Oracle Java 11 on CentOS and Fedora system follow below tutorial.

Step 1 – Download Latest Java Archive

The Oracle team provides Java RPM packages as well as compiled source code. Many times I have tried Java installation using rpm packages but I faced some issues. So I decided to install Java using the compiled source code. Since then I have installed a large number of times Java on CentOS, Redhat based systems without any issues. To download the latest Java SE Development Kit 8 release from its official download page or use following commands to download from the shell.

# cd /opt/
# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz"
# tar xzf jdk-8u201-linux-x64.tar.gz
If you do not have an opt folder, please create a folder with the mkdir command.

Step 2 – Install Java 8 with Alternatives

The alternatives command is used for maintained symbolic links. This command used to creates, removes, maintains and displays information about the symbolic links comprising the alternatives system. Let’s use the alternatives command to configure Java on your system. The alternatives command is available in chkconfig package.

#cd jdk1.8.0_201/
#alternatives --install /usr/bin/java java /opt/jdk1.8.0_201/bin/java 2
#alternatives --config java

The newly installed Java version listed at number 4, So input 4 and press enter.

There are 3 programs which provide ‘java’.

Selection Command

———————————————–
1 /opt/jdk1.8.0_45/bin/java
* 2 /opt/jdk1.8.0_144/bin/java
+ 3 /opt/jdk-11/bin/java
4 /opt/jdk1.8.0_201/bin/java

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

At this point, JAVA 8 has been successfully installed on your system. We also recommend to setup javac and jar commands path using alternatives

# alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_201/bin/jar 2
# alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_201/bin/javac 2
# alternatives --set jar /opt/jdk1.8.0_201/bin/jar
# alternatives --set javac /opt/jdk1.8.0_201/bin/javac

Step 3 – Check Installed Java Version

Java and javac binaries are available under PATH environment variable. You can use them from anywhere in your system. Let’s check the installed version of Java runtime environment (JRE) on your system by executing the following command.

# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

Step 4 – Setup Java Environment Variables

Most of Java based application’s uses environment variables to work. Set the Java environment variables using following commands

Set the JAVA_HOME, JRE_HOME and PATH environment variables.

# export JAVA_HOME=/opt/jdk1.8.0_201
# export JRE_HOME=/opt/jdk1.8.0_201/jre
# export PATH=$PATH:/opt/jdk1.8.0_201/bin:/opt/jdk1.8.0_201/jre/bin

Also add the above commands to /etc/bashrc or /etc/environment file to auto set environment variables during the system reboot.

Leave a Reply

Your email address will not be published. Required fields are marked *