How to install Java JDK and JRE on CentOS server

Reference Server

In this tutorial we will learn to install Java JDK and JRE on centOS server.

Prerequisite

It is assumed that you have wget installed.

Use the following command in the terminal to install wget.

yum install -y wget

Use sudo if you don't have the permissions.

Step 1 Download JRE

In this tutorial we will be installing JRE (Java Runtime Environment) version 7.

Open terminal and login to your server using ssh. Now cd to a directory where you want to download the files.

Lets say we have a Downloads directory and we want to download JRE in that folder.

Type the following command in the terminal to download JRE version 7.

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/7u79-b15/jre-7u79-linux-x64.rpm"

Output

[~]# cd Downloads
[Downloads]# 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/7u79-b15/jre-7u79-linux-x64.rpm"
--2016-12-12 05:07:20--  http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm
Resolving download.oracle.com (download.oracle.com)... 96.17.72.58, 96.17.72.66
Connecting to download.oracle.com (download.oracle.com)|96.17.72.58|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm [following]
--2016-12-12 05:07:21--  https://edelivery.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm
Resolving edelivery.oracle.com (edelivery.oracle.com)... 2600:1413:1:500:8300::2d3e, 2600:1413:1:500:8800::2d3e, 23.67.2.41
Connecting to edelivery.oracle.com (edelivery.oracle.com)|2600:1413:1:500:8300::2d3e|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm?AuthParam=1490850561_4973818e7b56fb9dd787998708cf1ae2 [following]
--2016-12-12 05:07:21--  http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jre-7u79-linux-x64.rpm?AuthParam=1490850561_4973818e7b56fb9dd787998708cf1ae2
Connecting to download.oracle.com (download.oracle.com)|96.17.72.58|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 33706160 (32M) [application/x-redhat-package-manager]
Saving to: ‘jre-7u79-linux-x64.rpm’

100%[==================================================================================================>] 33,706,160  3.52MB/s   in 9.1s   

2016-12-12 05:07:30 (3.52 MB/s) - ‘jre-7u79-linux-x64.rpm’ saved [33706160/33706160]

[Downloads]# ls -la
total 1028248
drwxr-xr-x  2 root root       4096 Apr 13 05:07 .
dr-xr-x---. 6 root root       4096 Apr 13 05:07 ..
-rw-r--r--  1 root root   33706160 Apr 13  2015 jre-7u79-linux-x64.rpm

Step 2 Install JRE

Use the following command in the terminal to install JRE.

yum install jre-7u79-linux-x64.rpm

use sudo if you don't have the permissions.

Output

[Downloads]# sudo yum install jre-7u79-linux-x64.rpm
Loaded plugins: fastestmirror
Examining jre-7u79-linux-x64.rpm: jre-1.7.0_79-fcs.x86_64
Marking jre-7u79-linux-x64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package jre.x86_64 0:1.7.0_79-fcs will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                   Arch                         Version                             Repository                                 Size
============================================================================================================================================
Installing:
 jre                       x86_64                       1.7.0_79-fcs                        /jre-7u79-linux-x64                        81 M

Transaction Summary
============================================================================================================================================
Install  1 Package

Total size: 81 M
Installed size: 81 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : jre-1.7.0_79-fcs.x86_64                                                                                                  1/1 
Unpacking JAR files...
	rt.jar...
	jsse.jar...
	charsets.jar...
	localedata.jar...
	jfxrt.jar...
  Verifying  : jre-1.7.0_79-fcs.x86_64                                                                                                  1/1 

Installed:
  jre.x86_64 0:1.7.0_79-fcs                                                                                                                 

Complete!
[Downloads]# 

Step 3 Download JDK

In this tutorial we will be downloading and installing JDK (Java Development Kit) version 7.

Use the following command to download JDK version 7.

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/7u79-b15/jdk-7u79-linux-x64.rpm"

Output

[Downloads]# 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/7u79-b15/jdk-7u79-linux-x64.rpm"
--2016-12-12 05:14:04--  http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm
Resolving download.oracle.com (download.oracle.com)... 96.17.72.66, 96.17.72.58
Connecting to download.oracle.com (download.oracle.com)|96.17.72.66|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm [following]
--2016-12-12 05:14:04--  https://edelivery.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm
Resolving edelivery.oracle.com (edelivery.oracle.com)... 2600:1413:1:500:8800::2d3e, 2600:1413:1:500:8300::2d3e, 23.67.2.41
Connecting to edelivery.oracle.com (edelivery.oracle.com)|2600:1413:1:500:8800::2d3e|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm?AuthParam=1490850965_eb153a76b759c1c9d185ee439e9de862 [following]
--2016-12-12 05:14:05--  http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm?AuthParam=1490850965_eb153a76b759c1c9d185ee439e9de862
Connecting to download.oracle.com (download.oracle.com)|96.17.72.66|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 138082565 (132M) [application/x-redhat-package-manager]
Saving to: ‘jdk-7u79-linux-x64.rpm’

100%[==================================================================================================>] 138,082,565 3.97MB/s   in 35s    

2016-12-12 05:14:41 (3.72 MB/s) - ‘jdk-7u79-linux-x64.rpm’ saved [138082565/138082565]

[Downloads]#

Step 4 Install JDK

To install JDK use the following command in the terminal.

yum install jdk-7u79-linux-x64.rpm

Use sudo if you don't have the permissions.

Output

[Downloads]# sudo yum install jdk-7u79-linux-x64.rpm
Loaded plugins: fastestmirror
Examining jdk-7u79-linux-x64.rpm: 2000:jdk-1.7.0_79-fcs.x86_64
Marking jdk-7u79-linux-x64.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package jdk.x86_64 2000:1.7.0_79-fcs will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================
 Package                  Arch                        Version                                Repository                                Size
============================================================================================================================================
Installing:
 jdk                      x86_64                      2000:1.7.0_79-fcs                      /jdk-7u79-linux-x64                      209 M

Transaction Summary
============================================================================================================================================
Install  1 Package

Total size: 209 M
Installed size: 209 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2000:jdk-1.7.0_79-fcs.x86_64                                                                                             1/1 
Unpacking JAR files...
	rt.jar...
	jsse.jar...
	charsets.jar...
	tools.jar...
	localedata.jar...
	jfxrt.jar...
  Verifying  : 2000:jdk-1.7.0_79-fcs.x86_64                                                                                             1/1 

Installed:
  jdk.x86_64 2000:1.7.0_79-fcs                                                                                                              

Complete!
[Downloads]#

Step 5 Set the environment

Now, open the .bash_profile file and set the JAVA_HOME and JRE_HOME.

[Downloads]# cd
[~]# ls -la
total 96
dr-xr-x---.  8 root root  4096 Dec 31 20:15 .
drwxr-xr-x  18 root root  4096 Dec 29 06:15 ..
-rw-------.  1 root root 11146 Dec 31 20:39 .bash_history
-rw-r--r--.  1 root root    18 Dec 29  2013 .bash_logout
-rw-r--r--   1 root root   429 Dec 31 10:38 .bash_profile

Open the .bash_profile file using vi or vim.

[~]# vi .bash_profile

And now add the following lines. Press i key to switch into INSERT mode.

export JAVA_HOME=/usr/java/jdk1.7.0_79
export JRE_HOME=/usr/java/jdk.7.0_79/jre

PATH=$HOME/bin:$JAVA_HOME/bin:$JRE_HOME:$PATH

Press Esc key to come out of the INSERT mode. Now type :wq to save the changes and exit.

Now, load the changes using source command.

[~]# source .bash_profile

All set, now check the Java version by typing the following command in the terminal.

[~]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)