Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible.

Java Development Kit (JDK)
This is an implementation of either one of the Java Platform, Standard Edition, Java Platform, Enterprise Edition, or Java Platform, Micro Edition platforms released by Oracle Corporation in the form of a binary product aimed at Java developers on Solaris, Linux, macOS or Windows.

In this guide, we will explain how to install the following variations of Java on a CentOS 7 server:

  • OpenJDK 8
  • OpenJDK 7
  • OpenJDK 6

To install Java on Ubuntu 16.04, you can follow the guide here

Deploying your cloud server
If you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can easily deploy your own cloud servers.

Once you have signed up, log into your Cloudwafer Client Area and deploy your Cloudwafer cloud server.

Updating System Packages
It is always recommended that you update the system to the latest packages before beginning any major installations. This is done with the command below:

sudo yum update

Install OpenJDK 8
This section will show you how to install the prebuilt OpenJDK 8 JRE and JDK packages using the yum package manager. OpenJDK 8 is the latest version of OpenJDK.

  • Install OpenJDK 8 JRE: Install OpenJDK 8 JRE using CentOS's package manager, yum by issuing these commands:

     sudo yum install java-1.8.0-openjdk
    

As a non-root user, you will be required to enter the password for the account in use. Enter the password and type y at the prompt to continue the installation.

At this stage, your OpenJDK 8 JRE has been installed.

  • Install OpenJDK 8 JDK: To install OpenJDK 8 JDK using CentOS's package manager, yum by running these commands:

    sudo yum install java-1.8.0-openjdk-devel
    

Type y at the prompt to continue the installation.
At this stage, you have installed OpenJDK 8 JDK successfully.

Install OpenJDK 7
This section will show you how to install the prebuilt OpenJDK 7 JRE and JDK packages using the yum package manager.

  • Install OpenJDK 7 JRE: Install OpenJDK 7 JRE using CentOS's package manager, yum by issuing these commands:

     sudo yum install java-1.7.0-openjdk
    

As a non-root user, you will be required to enter the password for the account in use. Enter the password and type y at the prompt to continue the installation.

At this stage, your OpenJDK 7 JRE has been installed.

  • Install OpenJDK 7 JDK: To install OpenJDK 7 JDK using CentOS's package manager, yum by running these commands:

    sudo yum install java-1.7.0-openjdk-devel
    

Type y at the prompt to continue the installation.
At this stage, you have installed OpenJDK 7 JDK successfully.

Install OpenJDK 6
This section will show you how to install the prebuilt OpenJDK 6 JRE and JDK packages using the yum package manager.

  • Install OpenJDK 6 JRE: Install OpenJDK 6 JRE using CentOS's package manager, yum by issuing these commands:

     sudo yum install java-1.6.0-openjdk
    

As a non-root user, you will be required to enter the password for the account in use. Enter the password and type y at the prompt to continue the installation.

At this stage, your OpenJDK 6 JRE has been installed.

  • Install OpenJDK 6 JDK: To install OpenJDK 6 JDK using CentOS's package manager, yum by running these commands:

    sudo yum install java-1.6.0-openjdk-devel
    

Type y at the prompt to continue the installation.
At this stage, you have installed OpenJDK 6 JDK successfully.

Checking Java Version
To check the version of Java that is currently set as the default, issue the command below:

java -version

Configuring the Default Java Version
From the above, we installed multiple versions of Java on our system. To set one as our default (i.e. the one that will run when a user runs the java command as some applications require certain environment variables to be set to locate which installation of Java to use.

Using Alternatives
The alternatives command, which manages default commands through symbolic links, can be used to select the default Java command.

To show the programs that provide the java command that is managed by alternatives, issue the command below:

sudo alternatives --config java

From the above, it asks you Enter to keep the current selection[+], or type selection number: to select another version.

Using Environment Variables
Numerous Java applications use the JAVA_HOME or JRE_HOME environment variables to determine which java executable to use.

If the java executable is located at /usr/java/jdk1.8.0_161/jre/bin/java, you could set your JAVA_HOME environment variable in a bash shell or script like so:

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk/jre

If you want JAVA_HOME to be set for every user on the system by default, add the previous line to the /etc/environment file. An easy way to append it to the file is to run this command:

sudo sh -c "echo export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk/jre >> /etc/environment"