VPS

How To Install Java on Ubuntu 16.04

Install Java on Ubuntu 16.04

Introduction

In this tutorial, we will show you how to install Java on a VPS running Ubuntu 16.04. We’ll also learn to set a Java home variable and overview some basic management steps to get you started.

Overall, Java is one of the leading programming languages in the world. It’s created to have as little dependencies as possible, which allows application developers to “write once, run anywhere.” As a result, a compiled Java code can operate on any platform which supports Java. You can utilize it to build anything from simple web applications to advanced software.

Without further ado, let’s see how we can install Java on Ubuntu 16.04!

Method 1 – Install Java on Ubuntu via default packages

The first method to install Java on Ubuntu is through the default packages. Begin the process by updating the current packages to the latest version:

apt-get update && apt-get upgrade

Once it finishes, install the latest version of Java Runtime Environment (JRE) by executing this command:

apt-get install default-jre

It is also possible to install Java Development Kit (JDK) instead. It is required by specific software or used to compile Java programs. JDK includes JRE, therefore there’s no disadvantage if you choose this method.

apt-get install default-jdk

That’s all! Java is ready for use on your Ubuntu machine. You can double check if it was properly set up with these commands:

java -version
javac -version

The output will be similar to:Checking if Java was properly installed on Ubuntu 16.04 via default packages

Method 2 – Install Java on Ubuntu via Oracle JDK

Alternatively, it is possible to install Java on Ubuntu using the official Oracle JDK. Begin by updating your current packages to the latest version:

apt-get update && apt-get upgrade

For this example, we’ll use a 3rd party library managed by WebUpd8. To implement it easier, install the following package first:

apt-get install software-properties-common

Next, get 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 the Java files. But it allows us to get the installers for Oracle Java software.

Finally, you may install Java on your Ubuntu machine by executing:

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

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

That’s it! Java is successfully installed. You may verify it by checking the version with these two commands:

java -version
javac -version

A similar output will appear:Checking if Java was properly installed on Ubuntu 16.04 via Oracle JDK

Managing Java

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

update-alternatives --config java

The following output will appear:

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

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

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

Enter the number from the selection menu which you wish to use as the default one. You can also do this with other Java commands, such as:

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

Here’s the syntax that will do the job:

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

Setting Java Home Environment

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

update-alternatives --config java

Path To Java Installation in Ubuntu 16.04Once you’ve copied the installation path, you will need to edit the environment file located in the etc directory:

nano /etc/environment

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

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

Don’t forget to update it with the actual path to your Java installation.

Press CTRL+X to finish editing and save the changes. Next, make sure the changes are applied with this command:

source /etc/environment

You can double check if it’s active by entering:

echo $JAVA_HOME

If you followed the instructions correctly the screen will prompt the JAVA_HOME variable that you entered:JAVA_HOME variable successfully changed

Conclusion

By finishing this tutorial, you have learned how to install Java on Ubuntu 16.04. You’ve also learned some basics, such as setting the default Java version and defining the JAVA_HOME variable.

If you found this article useful, feel free to share it with the world. And if you have any tips, suggestions, or ideas, we eagerly await them in the comments below!

Add Comment

Click here to post a comment

It's time to switch to a faster hosting for free.

No worries, it's one-click-easy to cancel at any time during your free trial.

  • FREE Domain Name
  • FREE Website Migration
  • 24/7 Professional Support
  • + much more

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!

More in VPS
Centos vs Ubuntu - Which one to choose for your web server or VPS
CentOS vs Ubuntu: Which One to Choose for Your Web Server

Close