How Install Java on CentOS 7 (Set java_home)

install java on CentOS 7

OpenJDK 7 is used as the default JDK version on CentOS 7. In this tutorial, I will show you how to install various versions of java on Centos 7.

I will cover both OpenJDK and Oracle Java installation in this guide. Basically both packages are technically the same where Oracle Java is more towards commercial purposes.

If you are just running a java program then you need only Java Runtime Environment (JRE). But if you are a java developer then you should install Java Development Kit (JDK) which contains all development tools.

Note: Java Runtime Environment is already available on JDK. If you already install JDK, no need to install JRE.

Install java on CentOS

Lets first check how to install OpenJDK JDK and OpenJRE. Java 11 is the current LTS version but Java 8 is most commonly used.

Install OpenJDK

The following command will install openjdk 8 using yum command:

$ sudo yum install java-1.8.0-openjdk-devel

If you are looking to install OpenJDK 11 follow this:

$ sudo yum install java-11-openjdk-devel

Install OpenJRE

The following command will install openjre 8 using yum command:

$ sudo yum install java-1.8.0-openjdk

For installalling OpenJRE 11 follow this:

$ sudo yum install java-11-openjdk

Install Oracle Java

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.

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

Here I am first uninstalling the previous version before I go for 8u11 version. It's up to you to decide whether to keep both versions.

$sudo rpm -qa | grep -i jdk
jdk-1.8.0_05-fcs.x86_64
$sudo rpm -evh jdk-1.8.0_05-fcs.x86_64
Preparing... ################################# [100%]
Cleaning up / removing...
1:jdk-2000:1.8.0_05-fcs ################################# [100%]
$sudo 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...

You can also use yum command to install oracle java as following:

sudo yum localinstall jre-VERSION-linux-x64.rpm

Set default Java version using alternatives tool

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.

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

$sudo 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.

$sudo 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

Check java version

Use the following -version command to check default java version.

$sudo 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)

Set java_home environment variable

Lets check path where java is installed and set java_home enironment variable.

$sudo 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
$sudo ls /usr/java/jdk1.8.0_11/jre/
bin lib plugin THIRDPARTYLICENSEREADME-JAVAFX.txt Welcome.html
COPYRIGHT LICENSE README THIRDPARTYLICENSEREADME.txt

Set for a 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.

Set Java Home

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

Set 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 for 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 $sudo source /etc/profile command to see the changes immediately or you need to logout & login.

Conclusion

In this tutorial, we learned how to install java on Centos 7 and set environment variables to start running your application. I hope you enjoyed reading this and if you have any questions or feedback, feel free to leave a comment.

Bobbin Zachariah 1:06 am

Comments

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