How to install Java on Ubuntu 20.04

In this guide, we will describe how to install Java on Ubuntu 20.04.

Java is one of the most popular programming languages in the world, and is used to build various applications and systems. Java runs on all major operating systems and devices. You can find applications developed with Java in your laptop, phone and game.

1. Before you start##

There are many different Java implementations. OpenJDK and Oracle Java are the two most important Java implementations. They are basically the same except that Oracle Java has very few additional features. The Oracle Java license is only allowed for use as non-commercial software, such as personal use and development use.

The default Ubuntu 20.04 source repository contains two OpenJDK packages, Java Runtime Environment (JRE) and Java Development Kit (JDK). JRE mainly includes Java Virtual Machine (JVM), classes and binary packages that allow you to run Java programs. JDK contains JRE and development/debugging tools and library files for building Java applications.

If you are not sure which version of Java to install, we usually recommend installing the OpenJDK (JDK 11) version. Some Java-based applications may need to run under a specified Java version, you should consult the application documentation.

Two, install OpenJDK 11

At the time of writing, Java 11 is a long-term support version (LTS) of Java. It is also the default Java development and runtime environment for Ubuntu 20.04.

Run the following command as root or another sudo user to update the package index and install the OpenJDK 11 JDK package:

sudo apt update
sudo apt install openjdk-11-jdk

Once the installation is complete, you can verify it by checking the Java version:

java -version

The output is similar to the following:

openjdk version "11.0.7"2020-04-14
OpenJDK Runtime Environment(build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM(build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

that's it! At this point, you have successfully installed Java on your Ubuntu system.

JRE is included in the JDK package. If you only need JRE, install the openjdk-11-jre package. Minimal Java runtime environment, install openjdk-11-jdk-headless package.

Three, install OpenJDK 8

Java 8, the previous version of Java LTS, is still widely used. If your application is running on Java 8, you can install it by typing the following command:

sudo apt update
sudo apt install openjdk-8-jdk

Verify the installation process by checking the Java version:

java -version

The output will look like this:

openjdk version "1.8.0_252"
OpenJDK Runtime Environment(build 1.8.0_252-8u252-b09-1ubuntu1-b09)
OpenJDK 64-Bit Server VM(build 25.252-b09, mixed mode)

Fourth, set the default version##

If you have installed multiple versions of Java on your Ubuntu system, you can enter the following command to check which version is set as the default:

java -version

To modify the default version, use the update-alternatives command:

sudo update-alternatives --config java

The output looks like this:

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

 Selection    Path                                            Priority   Status
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode
 1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

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

All installed Java versions will be listed. Enter the serial number you want to set as the default value, and press "Enter".

Five, JAVA_HOME environment variable##

In some Java applications, the environment variable JAVA_HOME is used to indicate the location of the Java installation.

To set the JAVA_HOME variable, first use update-alternatives to find the Java installation path:

sudo update-alternatives --config java

In this example, the installation path is as follows:

Once you find your preferred Java installation path, open the /etc/environment file:

sudo nano /etc/environment

Suppose you want to set JAVA_HOME to specify OpenJDK 11. At the end of the file, add the following line:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

To make the changes take effect in the current shell, you can log out and log in to the system again, or run the following command:

source /etc/environment

Verify that the JAVA_HOME environment variable is set correctly:

echo $JAVA_HOME

You should see the Java installation path:

/usr/lib/jvm/java-11-openjdk-amd64

Six, uninstall Java

You can uninstall Java using apt, just like uninstalling any software package.

For example, to uninstall the default-jdk package, enter:

sudo apt remove openjdk-11-jdk

Seven, summary##

Both OpenJDK 11 and OpenJDK 8 are in the default Ubuntu 20.04 software source repository and can be installed using the apt package management tool.

Recommended Posts

How to install Java on Ubuntu 20.04
How to install Java on Ubuntu 16.04 using Apt-Get
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 Protobuf 3 on Ubuntu
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 install MySQL on Ubuntu 18.04 (linux)