How to install Java on Ubuntu 16.04 using Apt-Get

Introduction

Java and JVM (Java Virtual Machine) are widely used and are required for a variety of software. This article will guide you through the process of installing and managing different versions of Java using apt-get.

Preparation

To follow this tutorial, you need:

Install the default JRE / JDK

The easiest way to install Java is to use the version packaged with Ubuntu. Specifically, this will install OpenJDK 8, which is the latest recommended version.

First, update the package index.

sudo apt-get update

Next, install Java. Specifically, this command will install the Java Runtime Environment (JRE).

sudo apt-get install default-jre

There is another default Java installation called JDK (Java Development Kit). If you want to compile Java programs or use Java software, you usually only need JDK.

JDK does include JRE, so in addition to the larger file size, if you install JDK instead of JRE, there are no disadvantages.

You can install the JDK with the following command:

sudo apt-get install default-jdk

Install Oracle JDK

If you want to install Oracle JDK (the official version distributed by Oracle), you need to perform several steps.

First, add Oracle's PPA, then update your package repository.

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

Then, depending on the version you want to install, execute one of the following commands:

Oracle JDK 8

This is the latest stable version of Java when it was written, and it is also the recommended installation version. You can do this with the following command:

sudo apt-get install oracle-java8-installer

Oracle JDK 9

This is a developer preview version, and the general version is scheduled to be released in March 2017. It is not recommended that you use this version because there may still be security issues and bugs.

To install JDK 9, use the following command:

sudo apt-get install oracle-java9-installer

Manage Java

Multiple Java can be installed on a server. You can use update-alternatives on the command line to configure the default version and manage which symbolic links are used for different commands.

sudo update-alternatives --config java

The output will look like the following. In this case, this is the output of installing all the Java versions mentioned above.

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:

You can now select the number you want to use as the default value. This can also be used for other Java commands, such as compiler (javac), document generator (javadoc), JAR signing tool (jarsigner), etc. You can use the following commands to fill in the commands you want to customize.

sudo update-alternatives --config command

Set JAVA_HOME environment variable

Many programs (such as Java servers) use the JAVA_HOME environment variable to determine where to install Java. To set this environment variable, we first need to find out where Java is installed. You can do this by executing the same command as in the previous section:

sudo update-alternatives --config java

Copy the path from the preferred installation and open /etc/environment with nano or your favorite text editor.

sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with the path you copied yourself.

JAVA_HOME="/usr/lib/jvm/java-8-oracle"

Save and exit the file, then reload.

source /etc/environment

You can now test whether the environment variables have been set by executing the following command:

echo $JAVA_HOME

This will return the path you just set.

in conclusion

You have now installed Java and know how to manage different versions of it. You can now install software that runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra or Jenkins.

To learn more about using Apt-Get to install Java related tutorials, please go to [Tencent Cloud + Community] (https://cloud.tencent.com/developer?from=10680) to learn more.


Reference: "How To Install Java with Apt-Get on Ubuntu 16.04"

Recommended Posts

How to install Java on Ubuntu 16.04 using Apt-Get
How to install Java on Ubuntu 20.04
How to install Oracle Java 14 on Ubuntu Linux
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Java on CentOS 8
How to install Bacula-Web on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install MySQL on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 20.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
How to install Docker Compose on Ubuntu 18.04
How to install Ubuntu on Raspberry Pi
How to install Bacula Server on Ubuntu 14.04
How to replace apt-get source on Ubuntu 18.04
How to install MySQL on Ubuntu 18.04 (linux)