+4 votes
203 views
How to install Java on Ubuntu 16.04

in Guides by (552k points)
reopened | 203 views

1 Answer

+5 votes
Best answer

Guide on how to install Java on Ubuntu 16.04
Define the version of Java that is used by default in Ubuntu.
How to configure the JAVA_HOME environment variable in Ubuntu 16.04
Install OpenJDK or Oracle JDK?

We all know to a greater or lesser extent what Java is and sooner or later we need to install it in our operating system for some type of software to work. This tutorial shows how to install Java on Ubuntu 16.04 step by step.

image

To show the steps to install Java I will use a VPS server to which I will connect through SSH using PuTTY . The idea of "‹"‹writing this guide arose when trying to install applications such as Elasticsearch, Cassandra or Tomcat on a server, since they all have the requirement to have Java installed.

Guide on how to install Java on Ubuntu 16.04

First of all we must clarify two terms that cause a lot of confusion when installing Java on our system, it is the terms JRE and JDK.

When we talk about JRE we refer to Java Runtime Environment , which could be defined as the libraries needed to run Java programs in an operating system. On the other hand we have the JDK, which means Java Development Kit and we need it when we are going to develop some type of Java application.

Having the above clear, we must install JRE or JDK according to our needs. If we install the JDK package it is not necessary to install JRE , because it is already included in the JDK package.

The easiest way to install Java on Ubuntu 16.04 is to use Ubuntu's own packages , which at the time of writing this article will install the OpenJDK 8 version that is the latest and recommended version.

The command to install JRE is:

sudo apt-get install default-jre

The command to install JDK is:

sudo apt-get install default-jdk

Install OpenJDK or Oracle JDK?

If we execute the commands shown above, this will install OpenJDK, but there is also Oracle JDK. There are no crucial differences between both packages, it is mostly licensing issues that led to differentiating them.

If we want to install the Oracle JDK package on Ubuntu 16.04 , we must add the official Oracle PPA repository with the command:

sudo add-apt-repository ppa:webupd8team/java

Now we update the package database with the command:

sudo apt-get update

In the PPA repository we find different Java versions available, but it is recommended to install the Oracle JDK 8 version which is considered stable at this time and for this we execute the command:

sudo apt-get install oracle-java8-installer

Define the version of Java that is used by default in Ubuntu.

There may be several versions of Java installed on our system, so it is sometimes necessary to select the version that will be used by default.

With the following command we can list all installed Java versions and establish which one we want to use by default:

sudo update-alternatives --config java

The same step must be followed to establish the version of the rest of the Java commands, such as the javac , the javadoc documentation generator, the jarsigner signing jarsigner , etc ... The command we would use would be the same as we showed before, only by changing the name of the java command that we want to configure, for example:

  • Set the default Java compiler version: sudo update-alternatives --config javac
  • Set the version of the Java documentation generator by default: sudo update-alternatives --config javadoc
  • Set the default JAR signing tool version: sudo update-alternatives --config jarsigner

How to configure the JAVA_HOME environment variable in Ubuntu 16.04

Many Java-based programs use the JAVA_HOME environment variable to determine where Java is installed. First of all we must know the directory where Java is installed, for this we can use the command sudo update-alternatives --config java that shows us on the screen the paths of the installed versions of Java.

In my case, the path shown on the screen is /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java and will be the one I set as the environment variable.

Now using the nano editor we open the environment variable configuration file, this is achieved by executing the command:

sudo nano /etc/environment

At the end of the file we add the line:

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

We save the file and leave the nano editor. For everything to work correctly we must reload the configuration of environment variables with the command:

source /etc/environment

A test that we can perform at the end to verify that the environment variable is correctly configured is to write the echo $JAVA_HOME command in the terminal and the path that we have configured should be displayed.

If we have done all the previous steps correctly we will already have Java perfectly installed and configured in our Ubuntu 16.04 operating system .


by (3.5m points)

Related questions

+3 votes
1 answer
+4 votes
1 answer
+5 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (552k points) | 936 views
+4 votes
1 answer
+3 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (552k points) | 1k views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users