How to install Java on Ubuntu 16.04

User onboarding: drive your customer to success
Why your site needs Schema Markup and how to add it in WordPress

In this tutorial, we’ll show how to install Java on a VPS Ubuntu 16:04. You will also learn how to set up an initial variable Java and see how to manage some basic steps to get you started.

Overall, Java is one of the world’s leading programming languages. It was designed to have the least possible dependencies, which allows you to develop applications such as “develop once, run anywhere”. As a result, Java compiled code can run on any platform that supports Java. You can use this to build things from simple to more advanced software.

So without further to say, we will know how to install Java on Ubuntu 16.04!

Method 1 – Installing Java on Ubuntu for a standard package

The first method to install Java on Ubuntu is through standard packages. Start the process by updating the current package to the latest version.

apt-get update && apt-get upgrade

When finished, install the latest version of Java Runtime Environment (JRE) by running the command:

apt-get install default-jre

You can also install Java Development Kit (JDK). The JDK is required for specific software or used to build Java programs. It also includes the JRE, then you have no disadvantage if you choose this method.

apt-get install default-jdk

That’s all! Java is ready to be used on your Ubuntu machine. You can check again if everything was set up correctly using the commands:

java -version
javac -version

You should find this result:checking java version

Method 2 – Installing Java on Ubuntu via Oracle JDK

Alternatively, see how to install Java on Ubuntu using the official Oracle JDK. Start updating your current package to the latest version

apt-get update && apt-get upgrade

For this example, we’ll use the third-party library managed by WebUpd8. To implement it even easier, install the following package:

apt-get install software-properties-common

Then, obtain the Java PPA with the following command:

add-apt-repository ppa: webupd8team / java

Note: This repository is not managed by Oracle and does not have Java files. But allows us to installers Oracle Java software.

Finally, you must install Java on your Ubuntu machine running the command:

apt update; apt-get install oracle-java9-installer

The above command will install the Java version 9. For an older version, you must change the syntax java9 to java8 and so on.

So that’s it! The java has been successfully installed! You can verify the installation if you want, just check the new version with these two commands:

java -version
javac -version

You will find the following:checking java version

Managing Java

The same server can have multiple Java installations. You can set the default version using the command line:

update-alternatives --config java

The response should appear as:

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

Path Selection Priority Status
-------------------------------------------------- ----------
 0 / usr / lib / JVM / java-7-oracle / bin / java auto mode 1071
 1 / usr / lib / JVM / java-8-openjdk-amd64 / JRE / bin / java manual mode 1081
 2 / usr / lib / JVM / java-8-oracle / JRE / bin / java manual mode 1081
3 * / usr / lib / JVM / java-9-oracle / bin / java manual mode 1091

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

Enter the default number you want to use as the menu displayed. You can also do this with other Java commands such as:

  • The compiler – javac
  • The documentation generator – javadoc
  • The JAR signing tool – jarsigner

Here is the syntax that will do this for you:

update-alternatives --config javac
update-alternatives --config javadoc
update-alternatives --config jarsigner

How to Set Java Home Environment

Another useful thing you need to know is how to set JAVA_HOME variations. Most applications require it to find the location of your Java installation. The previously used command can help you find the path of the Java installation.

update-alternatives --config java
Step by step to install Java 

When you finish copying the way, you’ll need to edit the file environment located within the directory etc :

nano / etc / environment

Add the following line at the end and set the value JAVA_HOME :

JAVA_HOME = "/ usr / lib / JVM / java-9-oracle / bin / java"

Do not forget to update to the current path to your Java installation.

Press CTRL + X to finish editing and save changes. Then, confirm that the changes were applied with the command:

source / etc / environment

You can check once again is up by typing:

echo $ JAVA_HOME

If you followed these instructions correctly, the screen will display the variable JAVA_HOME you entered:Java variable home


At the end of this tutorial, you will have learned how to install Java on Ubuntu 04.16. You also learned basic steps how to set the default version of Java and set the JAVA_HOME variable.

If you found this article useful, please feel free to share with the world. And if you have any tips, suggestions or idea, we look forward for your comment below!