Install Java SE on CentOS

Updated on December 2, 2015
Install Java SE on CentOS header image

Introduction

Java is a popular software platform that lets you develop and run Java applications and applets in various hardware environments.

There are three editions of the Java platform: Standard Edition (SE), Enterprise Edition (EE), and Micro Edition (ME). We will only talk about the Standard Edition (SE) of Java in this article.

There are also two different implementations of the Java SE platform: OpenJDK and Oracle Java. The source code of each implementation is almost the same, only several minor differences exist between them. In short, OpenJDK is fully open source and primarily GPL-licensed while Oracle Java adds some closed source third party components and some commercial features, using a commercial license - Binary Code License for Java SE Platform Products.

Besides, there are two different packages for each implementation of Java: Java Runtime Environment (JRE) and Java Development Kit (JDK). JRE is necessary for running compiled Java applications while JDK is used for developing Java applications.

Meanwhile, each implementation of Java SE has three widely-used version numbers (6, 7, and 8) for various kinds of applications.

For Oracle Java, the software packages can be 32-bit (i686, x86) or 64-bit (x64).

When installing Java SE, you need to pick the appropriate combination of the factors mentioned above, according to the requirements of your applications and licenses.

Prerequisites

All the commands in this tutorial are to be run by a non-root user with sudo privileges. You need to create such a user according to this article.

Notice

Usually, you just need to install only one of the following variants, but the design of Java allows you to install two or more variants on your system. After installation, you should also set up several environment variables for daily use. It also helps to setup the default Java program when you have multiple variants installed on your system. Instructions are provided later in this tutorial.

Installing OpenJDK 8 packages

JRE: sudo yum install -y java-1.8.0-openjdk

JDK: sudo yum install -y java-1.8.0-openjdk-devel

Installing OpenJDK 7 packages

JRE: sudo yum install -y java-1.7.0-openjdk

JDK: sudo yum install -y java-1.7.0-openjdk-devel

Installing OpenJDK 6 packages

JRE: sudo yum install -y java-1.6.0-openjdk

JDK: sudo yum install -y java-1.6.0-openjdk-devel

Installing Oracle Java 8 packages

Unlike OpenJDK, Oracle Java packages should be downloaded onto your system before you can install them.

There are three different packages of Oracle Java 8: JRE, Server JRE, and JDK. Among these packages, Server JRE package is for running Java applications on the server, the other two are used in the same way as their OpenJDK peers.

If possible, you should always download and install the latest version of Oracle Java, which is 8u66 at the time of writing. You can always find the latest version and its download URL on the Oracle Java official website.

Other installation options include: CPU types (i686 or x64) and download file types (.rpm or .tar.gz).

JRE

cd ~
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u66-b17/jre-8u66-linux-x64.rpm"
sudo yum localinstall -y jre-8u66-linux-x64.rpm
rm ~/jre-8u66-linux-x64.rpm

Note: If a newer version is released, replace the URL and the file name accordingly with the latest info on the Oracle website.

Server JRE

cd ~
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u66-b17/server-jre-8u66-linux-x64.tar.gz"
sudo mkdir /usr/java/
sudo tar -zxvf server-jre-8u66-linux-x64.tar.gz -C /usr/java/
rm ~/server-jre-8u66-linux-x64.tar.gz

Note: If a newer version is released, replace the URL and the file name accordingly with the latest info on the Oracle website.

Before you can use Java, you need to set up several environment variables:

sudo vi /etc/profile

Add the following sentences to the end of the file:

export JAVA_HOME=/usr/java/jdk1.8.0_66
export JRE_HOME=/usr/java/jdk1.8.0_66/jre
export PATH=$PATH:/usr/java/jdk1.8.0_66/bin

Save and quit:

:wq

Put your changes into effect:

source /etc/profile

JDK

cd ~
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" \
"http://download.oracle.com/otn-pub/java/jdk/8u66-b17/jdk-8u66-linux-x64.rpm"
sudo yum localinstall -y jdk-8u66-linux-x64.rpm
rm ~/jdk-8u66-linux-x64.rpm

Note: If a newer version is released, replace the URL and the file name accordingly with the latest info on the Oracle website.

About Oracle Java 7 and Oracle Java 6 packages

The security updates of Oracle Java 7 and Oracle Java 6 have been unavailable to the public for a while now. It's not recommended to continue the use of Oracle Java 7 or Oracle Java 6 without these security updates. If you are obligated to maintain an incompatible-to-Oracle-Java-8 legacy Java 7 or Java 6 application, you need to contact Oracle for further support.

Checking installation result

java -version

If your installation is OK, you will see a prompt like:

java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

Setting up environment variables

Whichever variant you have installed, you should set up the following environment variables for daily use. When multiple variants have been installed on your system, you can also specify the default Java program with the following method:

sudo vi /etc/profile

Add the following sentences to the end of the file. Replace /usr/java/jdk1.8.0_66 with the install location on your server:

export JAVA_HOME=/usr/java/jdk1.8.0_66
export JRE_HOME=/usr/java/jdk1.8.0_66/jre
export PATH=$PATH:/usr/java/jdk1.8.0_66/bin

Save and quit:

:wq

Put your changes into effect:

source /etc/profile