How to set up Java on Ubuntu!

How to set up Java on Ubuntu!
Photo by Battlecreek Coffee Roasters / Unsplash

Intro

Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers write once, and run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Click for more on Wikipedia.

Requirements

We need a machine with Ubuntu 16.04 installed for a non-root user with sudo authority in order to have Java working in your hand.

JRE/ OpenJDK Setup

First is first, let's get updates.

sudo apt-get update

We need JRE (Java Runtime Environment) for running java applications.

sudo apt-get install default-jre

If we want to develop any java code, we need to setup JDK (Java Development Kit);

sudo apt-get install default-jdk

Let's check java version;

java -version

Expected output;

openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-0ubuntu0.16.04.1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
You should see a newer version of java. When I write this article, the latest version was 1.8.

Oracle JDK Setup (if you prefer)

First of all, we need to install Oracle's PPA. For more detail about PPA

sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update

Java 8;

sudo apt-get install oracle-java8-installer

Java 9;

sudo apt-get install oracle-java9-installer

Default Java

For managing default java;

sudo update-alternatives --config java

(If you install 2 or more java);

There are 5 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      auto mode
  1            /usr/lib/jvm/java-6-oracle/jre/bin/java          1         manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          2         manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  4            /usr/lib/jvm/java-8-oracle/jre/bin/java          3         manual mode
  5            /usr/lib/jvm/java-9-oracle/bin/java              4         manual mode

Press <enter> to keep the current choice[*], or type selection number:

JAVA_HOME Environment Settings

Let's edit the environment file;

sudo nano /etc/environment

For example;

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java"

To refreshment;

source /etc/environment

Test JAVA_HOME variable;

echo $JAVA_HOME

The End

We currently have a fully working Java installation in our operating system. Also, we setup the JAVA_HOME environment variable. As of now we can run/develop java applications.