How Install java and Set Environment Variables RHEL / CentOS

December 2, 2012 | By
| Reply More

This article we illustrate how to install Java Development Kit 8 on RHEL/CentOS 7.0 version. As I am using 64 bit machine I would using 64 bit version of jdk. Below installation steps should work on other RPM based distributions such as RHEL 6.x, and Fedora etc. OpenJDK 7 is used as the default JDK version on RHEL/CentOS 7.0.

Note : This is our old article but updated with new version of jdk

JRE Family Version JRE Security Baseline
(Full Version String)
8 1.8.0_11
7 1.7.0_65
6 1.6.0_81
5.0 1.5.0_71

# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
#

Step 1: Download Java JDK / JRE 8u11

You can download Java SE Development Kit 8 from the Oracle download page for your specific OS and then upload to the server. Here I have used wget command to download the jdk-8u11-linux-x64.rpm file.

# wget http://download.oracle.com/otn-pub/java/jdk/8u11-b12/jdk-8u11-linux-x64.rpm

# ls
jdk-8u11-linux-x64.rpm
#

Step 2: Install Java 8u11

Here I am first uninstalling the previous version before I go for 8u11 version. Its upto you to decide whether to keep both version.

# rpm -qa | grep -i jdk
jdk-1.8.0_05-fcs.x86_64
# rpm -evh jdk-1.8.0_05-fcs.x86_64
Preparing... ################################# [100%]
Cleaning up / removing...
1:jdk-2000:1.8.0_05-fcs ################################# [100%]
#

## rpm -Uvh jdk-8u11-linux-x64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:jdk-2000:1.8.0_11-fcs ################################# [100%]
Unpacking JAR files...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...
#

Alternatives

Alternatives is a tool for managing different software packages that provide the same functionality. Linux uses alternatives to ensure that only one Java Development Kit (JDK) is set as default at a time.

# gunzip /usr/local/jdk-8u11-linux-x64.gz
# tar -xvf jdk-8u11-linux-x64
# cd /usr/local/

# alternatives --install /usr/bin/java java /usr/local/jdk1.8.0_11/bin/java 3

This will assign Oracle JDK a priority of 3. You can give Oracle JDK the higher priority if you want it to remain the default.

# alternatives --config java

There are 2 programs which provide 'java'.

Selection Command
-----------------------------------------------
*+ 1 /usr/local/jdk1.7.0_65/bin/java
2 /usr/local/jdk1.8.0_11/bin/java

Enter to keep the current selection[+], or type selection number: 2
#

java install

Step 3: Check Version of Java

# java -version
java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
#

Step 4: Setup Environment Variables

# ls /usr/java/jdk1.8.0_11/
bin javafx-src.zip man THIRDPARTYLICENSEREADME-JAVAFX.txt
COPYRIGHT jre README.html THIRDPARTYLICENSEREADME.txt
db lib release
include LICENSE src.zip
# ls /usr/java/jdk1.8.0_11/jre/
bin lib plugin THIRDPARTYLICENSEREADME-JAVAFX.txt Welcome.html
COPYRIGHT LICENSE README THIRDPARTYLICENSEREADME.txt
#

Set for single user

If you are root user edit .bash_profile of the specific user (example in /home/bobbin/.bash_profile) or login with user account. And add below lines to the file. You  replace the jdk version path depending on your installation.

##Setting Java Home

export JAVA_HOME=/usr/java/jdk1.8.0_11/

##Setting JRE Home

export JRE_HOME=/usr/java/jdk1.8.0_11/jre

##Export PATH Variable for JAVA Home and JRE Home

export PATH=$PATH:/usr/java/jdk1.7.0_65/bin:/usr/java/jdk1.8.0_11/jre/bin

or

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

 Set globally or all users

To set globally you need set java path in /etc/profile file. Append below lines into the file

export PATH=$PATH:/usr/java/jdk1.8.0_11/bin

You need run # source /etc/profile command to see the changes immediately or you need to logout & login.

Filed Under : LINUX HOWTO

Tagged With :

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.