2 Ways of installing Java 8 on CentOS

Installing Java on CentOS is one of the easiest exercise ever. However, I’ll let you know two different ways in which Java can be installed on CentOS. We’ll take latest version of Java which is Java 8. Even you can combine all following steps in one shell script and can simply execute that shell script in order to install Java/JDK. Following steps are performed using root user you can also execute the steps with non root user having sudoer rights. Before moving towards installation process I have assumed that CentOS has wget command installed. If not than install it using below step.

$ yum install wget

Note: Refer my post How to setup Passwordless SSH with non root users in CentOS to know how to create non root user in linux.

Ways of Installation

  • Tarball Instllation
  • RPM Instllation
Download Tar OR RPM file of Java 8 using below commands
$ cd /data
$ 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/8u40-b25/jdk-8u40-linux-x64.tar.gz"

OR

$ cd /data
$ 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/8u40-b25/jdk-8u40-linux-x64.rpm"
For Tarball Install,  Extract & Move jdk1.8.0_40 directory under /usr/java/ directory
$ tar –xvf /data/jdk-8u40-linux-x64.tar.gz
$ mkdir mkdir /usr/java
$ mv /data/jdk1.8.0_40 /usr/java/
For RPM Install, Extract RPM file with following command
$ rpm -ivh /data/jdk-8u40-linux-x64.rpm

Than after All subsequent Commands are same :)

Configure java, javac, jar, jps and javaws commands using alternatives

$ alternatives --install /usr/bin/java java /usr/java/jdk1.8.0_40/bin/java 2 
$ alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.8.0_40/bin/javaws 2 
$ alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_40/bin/javac 2 
$ alternatives --install /usr/bin/jar jar /usr/java/jdk1.8.0_40/bin/jar 2 
$ alternatives --install /usr/bin/jps jps /usr/java/jdk1.8.0_40/bin/jps 2

Note: alternatives is a command in linux which is used to create, maintain, remove and displays information related to the symbolic links comprising the alternatives system. Click here to read more about alternatives

Check Java version
$ java -version
java version "1.8.0_40"
Java(TM) SE Runtime Environment (build 1.8.0_40-b25)
Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)
 Update JAVA_HOME and PATH for all users in any of below files

/etc/profile (Global for all users)

$ echo "" >> /etc/profile
$ echo "## Setting JAVA_HOME and PATH for all USERS ##" >> /etc/profile
$ echo "export JAVA_HOME=/usr/java/jdk1.8.0_40" >> /etc/profile
$ echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> /etc/profile
$ source /etc/profile

~/.bash_profile (User specific)

$ echo "" >> ~/.bash_profile
$ echo "## Setting JAVA_HOME and PATH for all USERS ##" >> ~/.bash_profile
$ echo "export JAVA_HOME=/usr/java/jdk1.8.0_40" >> ~/.bash_profile
$ echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> ~/.bash_profile
$ source ~/.bash_profile

That’s it we are done with this simplest steps of installation of Java 8 on CentOS….!!!!!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>