Blog

03/06/2019

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

Posted by: SXI ADMIN

The author is the creator of nixCraft 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.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...