Install Java (JRE or JDK) in Linux

Introduction

There are two standard types of installations, JRE (Java Runtime Environment) and JDK (Java Development Kit). JRE enables the ability to create Java Applications for different types of deployments using minimal core tools to accomplish the task. JDK is a fully loaded Development Kit that has everything that JRE has plus additional resources to create/secure Applications and Applets.

Prerequisites

Before we begin, let make sure that the server is fully updated with the following command:

sudo apt-get update

After your server has been fully updated, you will need to know what version of Java is currently installed or if it is not installed, with the following command:

java -version

Install Java Open JRE or JDK on Ubuntu

Once you have verified if Java is installed or not, choose the type of Java installation that you want with one the following:

sudo apt-get install default-jdk
sudo apt-get install default-jre

Install Java Oracle JRE or JDK on Ubuntu

Another alternative Java install is with Oracle JRE and JDK. However, we would need to install additional repositories for a proper installation.

sudo add-apt-repository ppa:webupd8team/java

Then, you will need to fully update the system with the following command:

sudo apt-get update

You can newer versions of Java by using one of the following commands:

sudo apt-get install oracle-java9-installer

If you have multiple versions of Java installed on your server, then you have the ability to select a default version. Check your alternatives with the following command:

sudo update-alternatives --config java

or
sudo apt install oracle-java9-set-default

Setup JAVA_HOME on Ubuntu

Since many programs now days need a JAVA_HOME environment variable to work properly. We will need to find the appropriate path to make these changes. To edit the environment file use your text editor and edit the following file:

sudo nano ~/.bashrc

Now that you are in the user profile file, add the following code, along with the Path of your installation from the previous step, to the bottom. ( Example: JAVA_HOME=”YOUR_PATH”)

export JAVA_HOME="/usr/lib/jvm/java-9-oracle"