CentOS Install Java SDK using yum Command

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

[donotprint][/donotprint]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

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 SXI LLC!");

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 SXI LLC!

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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.