≡ 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-
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-
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:
{ 10 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.

  • nitin September 24, 2016, 6:00 am

    while executing $HOME/.bashrc as a root I am getting error like
    bash: /root/.bashrc: Permission denied

    Please help how to overcome this

  • ranjeet October 13, 2016, 5:15 am

    Oct 13, 2016 10:45:49 AM org.apache.catalina.startup.HostConfig deployDescriptor
    INFO: Deploying configuration descriptor gslenvosys.xml
    Oct 13, 2016 10:45:52 AM org.apache.tomcat.util.modeler.Registry registerComponent
    SEVERE: Null component Catalina:type=JspMonitor,name=jsp,WebModule=//localhost/gslenvosys,J2EEApplication=none,J2EEServer=none
    Oct 13, 2016 10:45:52 AM org.apache.catalina.startup.HostConfig deployDescriptor
    SEVERE: Error deploying configuration descriptor gslenvosys.xml
    java.lang.NoSuchMethodError: org.apache.naming.NamingContextEnumeration.(Ljava/util/Iterator;)V
    at org.apache.naming.resources.FileDirContext.list(FileDirContext.java:306)
    at org.apache.naming.resources.ProxyDirContext.list(ProxyDirContext.java:487)
    at org.apache.catalina.startup.TldConfig.tldScanResourcePathsWebInf(TldConfig.java:515)
    at org.apache.catalina.startup.TldConfig.tldScanResourcePaths(TldConfig.java:484)
    at org.apache.catalina.startup.TldConfig.execute(TldConfig.java:257)
    at org.apache.catalina.startup.TldConfig.lifecycleEvent(TldConfig.java:634)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4377)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:637)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:563)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:498)
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1345)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590)
    at java.lang.Thread.run(Thread.java:745)

  • ranjeet October 13, 2016, 5:17 am

    While starting Tomcat i got the below error of loadng Context file.
    Please help

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , ,