Home » Java » How to install Java SDK on CentOS?

How to install Java SDK on CentOS?

Posted by: admin November 30, 2017 Leave a comment


I have CentOS 5, but I don’t know the steps to install Java SDK on Linux.

Where to download the RPM file and what can I do next to fully install that?

Then I need to install Tomcat.

Or is there any ready-made package for all?


The following command will return a list of all packages directly related to Java. They will be in the format of java-<version>.

$ yum search java | grep 'java-'

If there are no available packages, then you may need to download a new repository to search through. I suggest taking a look at Dag Wieers’ repo. After downloading it, try the above command again.

You will see at least one version of Java packages available for download. Depending on when you read this, the lastest available version may be different.


The above package alone will only install JRE. To also install javac and JDK, the following command will do the trick:

$ yum install java-1.7.0-openjdk*

These packages will be installing (as well as their dependencies):



@Sventeck, perfecto.

redhat docs is always a great source – good tutorial that explains install JDK via yum and then setting the path can be found here (have fun!) – Install OpenJDK and set $JAVA_HOME path

OpenJDK 6:

# yum install java-1.6.0-openjdk-devel

OpenJDK 7:

# yum install java-1.7.0-openjdk-devel

To list all available java openjdk-devel packages try:

# yum list | grep -i "java-.*openjdk-devel"


On centos 7, I just do

sudo yum install java-sdk

I assume you have most common repo already.
Centos just finds the correct SDK with the -devel sufix.


An alternative answer is,

sudo yum list \*java-1\* | grep open 

than select one from list and install that

for example,

sudo yum install java-1.7.0-openjdk.x86_64


yum install java-1.8.0

and then:

alternatives --config java

and check:

java -version


Here is a detailed information on setting up Java and its paths on CentOS6.

Below steps are for the installation of latest Java version 8:

  1. Download java rpm package from Oracle site. (jdk-8-linux-x64.rpm)
  2. Install from the rpm. (rpm -Uvh jdk-8-linux-x64.rpm)
  3. Open /etc/profile, and set the java paths, save it.
  4. Check the java installation path, and java version, with the commands: which java, java -version

Now you can test the installation with a sample java program


If you want the Oracle JDK and are willing not to use yum/rpm, see this answer here:

Downloading Java JDK on Linux via wget is shown license page instead

As per that post, you can automate the download of the tarball using curl and specifying a cookie header.

Then you can put the tarball contents in the right place and add java to your PATH, for example:

curl -v -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u45-b14/jdk-8u45-linux-x64.tar.gz > jdk.tar.gz

tar xzvf jdk.tar.gz
sudo mkdir /usr/local/java
sudo mv jdk1.8.0_45 /usr/local/java/
sudo ln -s /usr/local/java/jdk1.8.0_45 /usr/local/java/jdk

sudo vi /etc/profile.d/java.sh
export PATH="$PATH:/usr/local/java/jdk/bin"
export JAVA_HOME=/usr/local/java/jdk

source /etc/profile.d/java.sh


Since Oracle inserted some md5hash in their download links, one cannot automatically assemble a download link for command line.

So I tinkered some nasty bash command line to get the latest jdk download link, download it and directly install via rpm.
For all who are interested:

wget -q http://www.oracle.com/technetwork/java/javase/downloads/index.html -O ./index.html && grep -Eoi ‘]+>’ index.html | grep -Eoi ‘/technetwork/java/javase/downloads/jdk8-downloads-[0-9]+.html’ | (head -n 1) | awk ‘{print “http://www.oracle.com“$1}’ | xargs wget –no-cookies –header “Cookie: gpw_e24=xxx; oraclelicense=accept-securebackup-cookie;” -O index.html -q && grep -Eoi ‘”filepath”:”[^”]+jdk-8u[0-9]+-linux-x64.rpm“‘ index.html | grep -Eoi ‘http:[^”]+’ | xargs wget –no-cookies –header “Cookie: gpw_e24=xxx; oraclelicense=accept-securebackup-cookie;” -q -O ./jdk8.rpm && sudo rpm -i ./jdk8.rpm

The bold part should be replaced by the package of your liking.


use the below commands to install oracle java8 through terminal

Step -1) Visit Oracle JDK download page, look for RPM version

Step -2) Download oracle java 8 using the below command
wget –header “Cookie: oraclelicense=accept-securebackup-cookie” http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.rpm

Step -3) Install the java8 using below command
sudo yum localinstall jdk-8u121-linux-x64.rpm
Now the JDK should be installed at
Step -4) Remove the downloaded .rpm file to utilize the space.
rm jdk-8u121-linux-x64.rpm

Step -5) Verify the java by using command
java -version

Step -6) If the CentOS has multiple JDK installed, you can use the alternatives command to set the default java
sudo alternatives –config java

Step -7)Optional set JAVA_HOME Environment variables.
copy the path of jdk install i.e /usr/java/jdk1.8.0_121
use below command to export java home
export JAVA_HOME=/usr/java/jdk1.8.0_121


I have written a shell script to install/uninstall java on centos.
You can get it done by just run the shell.
The core of this shell is :

1.download the jdk rpm(RedHat Package Manager) package.
2.install java using rpm.

You can see more detail here:

Hope it works for you.

Leave a Reply

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