≡ Menu

CentOS Install Java SDK using yum Command

How do I install JDK runtime environment on a CentOS Linux version 6.x using yum command?

CentOS 6.x and 5.x comes with the OpenJDK runtime environment. It is an open-source implementation of the Java Platform.

CentOS Linux JAVA package names

  1. java-1.7.0-openjdk – OpenJDK Runtime Environment
  2. java-1.7.0-openjdk-devel – OpenJDK Development Environment

To see a list of all available JDK on the system type the following command:
# yum search java | grep -i --color JDK
Sample outputs:

Fig. 01: Listing JDKs on CentOS/RHEL

Fig. 01: Listing JDKs on CentOS/RHEL

Install Java SDK on a CentOS Linux

Open the Terminal and type the following yum command to install OpenJDK as root user:
# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel
Sample outputs:

 

Set the JAVA_HOME environment variable on a CentOS Linux

RHEL and CentOS Linux install OpenJDK into /usr/lib/jvm/ directory:
# ls -l /usr/lib/jvm/
Sample outputs:

total 4
drwxr-xr-x. 3 root root 4096 Feb 24 17:00 java-1.7.0-openjdk-1.7.0.51.x86_64
lrwxrwxrwx. 1 root root   21 Feb 24 17:00 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root   27 Feb 24 17:00 jre-1.7.0 -> /etc/alternatives/jre_1.7.0
lrwxrwxrwx. 1 root root   38 Feb 24 17:00 jre-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.51.x86_64/jre
lrwxrwxrwx. 1 root root   29 Feb 24 17:00 jre-openjdk -> /etc/alternatives/jre_openjdk

Set the JAVA_HOME to the directory containing a bin/java executable using export command:
# export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
Open the shell configuration file such as $HOME/.bashrc and at the bottom of the file, type the following line:

export JAVA_HOME="/usr/lib/jvm/jre-1.7.0-openjdk.x86_64"

Save and close the file. See “How to Set JAVA_HOME / PATH variables Under Linux Bash Profile” tutorial for more information.

Test your Java installation

Create a program called HelloWorld.java as follows:
$ cat > HelloWorld.java
Sample code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World! This is a test code by nixCraft!");
    }
}

Save and close the file. Compile and run it as follows:
$ javac HelloWorld.java
$ java HelloWorld

Sample outputs:

Hello, World! This is a test code by nixCraft!

How do I run a .Jar java file based app?

The syntax is:
$ java -jar file.jar
$ java -jar /path/to/my/java/app.jar
$ java -jar /path/to/my/java/app.jar arg1 arg2

See also
Share this tutorial on:

{ 7 comments… add one }
  • Jason April 21, 2014, 9:34 pm

    I can’t seem to get around some dependencies that I cannot find.

    –> Processing Dependency: python-xml for package: javapackages-tools-2.0.1-25.1.x86_64
    –> Processing Dependency: libxslt-tools for package: javapackages-tools-2.0.1-25.1.x86_64
    –> Finished Dependency Resolution
    Error: Package: javapackages-tools-2.0.1-25.1.x86_64 (Education)
    Requires: python-xml
    Error: Package: javapackages-tools-2.0.1-25.1.x86_64 (Education)
    Requires: libxslt-tools
    You could try using –skip-broken to work around the problem
    You could try running: rpm -Va –nofiles –nodigest

    Any advice?

    • Nix Craft April 22, 2014, 6:10 am

      Can you state your version of CentOS Linux system? This tutorial only tested on Centos 6.x (may work on centos 5.x too).

  • Younes December 9, 2014, 7:36 pm

    Thanks a lot for this discription ;) but u must in the end see of installation the version of ur java by using #java -version that command line must put out in terminal something like this : Java TM et Java Hotspot
    u can get that by using # alternatives –config java

  • Muhammad Aslamkhan October 4, 2015, 9:28 am

    Thanks a lot. It helps me installing the jdk on my CentOS.
    once again thanks.

  • Ostap November 9, 2015, 7:40 pm

    Thank you very much! Very useful guide!

  • Muhamed Fuad December 14, 2015, 1:28 pm

    Thanks for the Guide. I tried so many ways to install java. But this one only make useful.

Leave a Comment


   Tagged with: , , , , , ,