setup java

How to setup Java on Ubuntu 18.04

Most of the devices we use run on JAVA. Therefore, in this CloudSigma tutorial, you will learn how to setup JAVA on Ubuntu 18.04 server.

JVM (JAVA Virtual Machine) is required to run various kinds of software like Jenkins, Tomcat, etc. In addition, JAVA Development Kit (JDK) is used to compile and run some specific JAVA Based Softwares. Using the tutorial, you can choose and install any specific version of JRE and JDK. You can then use it to run various applications or develop new software.

Creating the machine

As a first step, you have to create a machine. You can do that easily on CloudSigma WebApp’s compute section.
Create a Linux Server setup java

For the purposes of the tutorial, you can use the following resources:
CPU: 8 GHz
RAM: 8 GB
SSD: 50 GB

You can mount the disk with Ubuntu 18.04 LTS image available in CloudSigma’s library:
Ubuntu 18.04: Pre-installed 64bit with VirtIO drivers, superuser, Python 2.7.17, Pip 20.0.2 OpenSSL 1.1.1d, Cloud-init and latest updates until 2019-03-03.

After starting the machine, you can update all the existing repos and packages on the machine by running the following commands:

Installing the JRE/JDK

The simplest way to install JAVA is to install the version packaged with Ubuntu using ‘apt’. Ubuntu 18.04 includes OpenJDK Version 11, which is an open-source variant of JAVA.

First, you should run the following command to check if JAVA is already installed on your server:

You will be able to see this output if JAVA is not installed on your server :

Next, you can run the following command to install the Java Runtime Environment once you have confirmed that JAVA is not installed:

Verify the installation by checking the version of JAVA:

Some software requires Java Development Kit (JDK) to compile and run. You can run the following command to install it:

Verify the installation by checking its version:

JRE - JDK Version Check

Now that you have learned how to install the open-sourced version of JAVA, you can move to the next section if you want to learn how to setup Oracle JDK.

Installing Oracle JDK 14

Since Oracle’s licensing doesn’t allow installations through package managers, you will have to use a third party script/installer to install JAVA.

To install the Oracle’s JDK, you can add the repo of a linuxuprising and update the packages. After that you can run the install command:

Checking the installed versions

Managing different JAVA Versions

If you have multiple JAVA versions installed on one server, you can configure one of them to be the default. In order to do this, use the following command:

Changing Default JAVA Version

You can choose the number associated with the preferred JAVA version. Press the ‘Enter’ key to configure the selected version as the default one.

You can also do the same for the JAVA compiler:
Change Default Version of javac

Setting Environment Variable – JAVA_HOME

A lot of programs that require JAVA find the installation location from “JAVA_HOME” environment variable. To set the variable, you need to find the location of installation. You can do that by using the update-alternatives command:

Copy the path of the required JAVA version and put it in the below command:

Next, for the settings to take effect, you need to reload the environment file using the command:

After that, you can execute the following command to verify if the variable is set :

set java_home

Users working on the server will have to log out and log in for this change to take effect.

Now that you know how to setup JAVA on Ubuntu and manage its different versions, you are all set to install various programs that need JAVA.

Happy Computing!