How to Install Java on CentOS 7

How to install Java on CentOS 7
How to install Java on CentOS 7

Install Java on CentOS 7

Java is the popular programming language owned by Oracle. Java is used to build cross-platform applications. Over 3 billion devices run on Java. In this tutorial, we are going to install Java on CentOS 7 using different methods.

Java has following three editions:

  1. Standard Edition(SE).
  2. Micro Edition(ME).
  3. Enterprize Edition(EE).

Following two implementations available for Java:

  1. Oracle Java.
  2. OpenJDK Java.

In this tutorial we are going to install Standard Edition (SE). There is no more difference in Oracle Java and OpenJDK Java as Oracle Java provides some additional enterprise features.

Prerequisites

Before you start to install Java on CentOS 7. You must have the non-root user account on your system with sudo privileges.

You should have curl package installed on your system. We are going to use curl package to download Oracle JRE/JDK from their official website. Install it by using following command.

sudo yum install curl

Install OpenJDK 8

Currentally, OpenJDK 8 is the latest LTS version of OpenJDK. Here we are going to install it with yum package manager.

Install OpenJDK 8 JRE

To install OpenJDK 8 JRE package run following command.

NOTE : If you have installed OpenJDK 8 JDK then we do not need to install OpenJDK 8 JRE As JRE is the subset of JDK.

sudo yum install java-1.8.0-openjdk

Install OpenJDK 8 JDK

Run the following command to install OpenJDK 8 using yum package manager:

sudo yum install java-1.8.0-openjdk-devel

Confirm the installation and check the version typing following command.

java -version

Install OpenJDK 7

Install OpenJDK 7 JDK

Run the following command to install OpenJDK 7 using yum package manager:

sudo yum install java-1.8.0-openjdk-devel

Install OpenJDK 7 JRE

To install OpenJDK 7 JRE package run following command.

NOTE : If you have installed OpenJDK 7 JDK then we do not need to install OpenJDK 8 JRE As JRE is the subset of JDK.

sudo yum install java-1.8.0-openjdk

Install Oracle Java 8

At the time writing this tutorial Oracle Java 8 is the latest LTS version of Oracle Java. Now to install Oracle Java 8 follow the instractions given below.

Install Oracle JRE 8

To download and install Oracle JRE 8 run following commands:

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jre-8u162-linux-x64.rpm
sudo yum localinstall jre-8u162-linux-x64.rpm

Install Oracle JDK 8

Now to install Oracle JDK 8 you can run following commands:

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/8u162-b12/0da788060d494f5095bf8624735fa2f1/jdk-8u162-linux-x64.rpm
sudo yum localinstall jdk-8u162-linux-x64.rpm

Install Oracle Java 9

If you want to install Oracle Java 9 then you can use following instractions.

Install Oracle JRE 9

To download and install Oracle JRE 9 run following commands:

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/9.0.4+11/c2514751926b4512b076cc82f959763f/jre-9.0.4_linux-x64_bin.rpm
sudo yum localinstall jre-9.0.4_linux-x64_bin.rpm

Install Oracle JDK 9

Now to download and install Oracle JDK 9 you can run following commands:

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/9.0.4+11/c2514751926b4512b076cc82f959763f/jdk-9.0.4_linux-x64_bin.rpm
sudo yum localinstall jdk-9.0.4_linux-x64_bin.rpm

Setting up default Java Version

If you have installed multiple versions of Java on your system then you can change the default version running following command.

Check Current Java Version running following command.

java -version

Now to change default Java version run following command.

sudo alternatives --config java

The output should be:

There is 1 choice for the alternative java (providing /usr/bin/java).

  Selection    Path                                           Priority   Status
---------------------------------------------------------------------------------------
  0            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      auto mode
  1            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode
* 2            /usr/local/oracle-java-10/jdk-10.0.2/bin/java    1500      manual mode

Press  to keep the current choice[*], or type selection number:

NOTE: To change version just enter the number in the selection column and hit ENTER

Conclusion

You have successfully learned How to install Java on CentOS 7. If you have any queries regarding this then please don’t forget to comment below.