How to

How to Install Java on Ubuntu 20.04

How to Install Java on Ubuntu 20.04

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

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

Before you begin

There are several different Java implementations. OpenJDK and Oracle Java are the two main implementations of Java, with almost no difference between them except that Oracle Java has several additional commercial features. The Oracle Java License only permits non-commercial use of software, such as personal use and development use.

The default Ubuntu 20.04 repository includes two OpenJDK packages, Java Runtime Environment (JRE) and Java Development Kit (JDK). JRE consists of Java virtual machines (JVM), classes, and binaries that allow you to run Java programs. JDK includes JRE and development / debugging tools and libraries needed to build Java applications.

If you are not sure which Java package to install, the general recommendation is to install the default version of OpenJDK (JDK 11). Some Java-based applications may require certain versions of Java, so you should read the application’s documentation.

Installing OpenJDK 11

At the time of writing, Java 11 is the latest supported long-term version of Java. This is also the default Java development and runtime on Ubuntu 20.04.

Run the following command as a user with sudo or root privileges to update the package index and install the JJK OpenJDK 11 package:
sudo apt update
sudo apt install openjdk-11-jdk
After the installation is complete, you can verify it by checking the Java version:

java -version

The output will look like this:

Output :

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)

There she is! 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. For a minimal Java runtime, install the openjdk-11-jdk-headless package.

Install OpenJDK 8

Java 8, the previous version of Java LTS, is still widely used. If your application runs on Java 8, you can install it by typing the following command:
sudo apt update
sudo apt install openjdk-8-jdk
Verify the installation by checking the Java version:

java -version

The output will look like this:

Output :

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)

Set Default Version

If you have several versions of Java installed on your Ubuntu system, you can check which version is set as default by typing:

java -version

To change the default version, use the alternative-update command:

sudo update-alternatives --config java

The output will look like this:

Output :

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: 

You will be presented with a list of all installed Java versions. Enter the version number that you want to use as the default and press Enter.

JAVA_HOME Environmental Variable

The JAVA_HOME environment variable is used by several Java applications to determine the location of a Java installation.

To set the JAVA_HOME variable, first find the Java installation path with alternative-updates:

sudo update-alternatives --config java

In this example, the installation path is as follows:

  • OpenJDK 11 is located at / usr / lib / jvm / java-11-openjdk-amd64 / bin / java
  • OpenJDK 8 is located at / usr / lib / jvm / java-8-openjdk-amd64 / jre / bin / java

After you find the Java installation path of your choice, open the / etc / environment file:

sudo nano /etc/environment

Assuming you want to set JAVA_HOME to point to OpenJDK 11, add the following line, at the end of the file:

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

For changes to take effect on your current shell, you can exit and enter or run the following source commands:

source /etc/environment

Verify that the JAVA_HOME environment variable is set correctly:

echo $JAVA_HOME

You will see a path to the Java installation:

Output :

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

/etc/environment is a system-wide configuration file, which is used by all users. If you want to set the JAVA_HOME variable on a per-user basis, then add the line to the .bashrc or any other configuration file which is loaded when the user logs in.

Uninstalling Java

You can delete Java like other packages installed with apt.

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

sudo apt remove openjdk-11-jdk

Conclusion

OpenJDK 11 and OpenJDK 8 are available in the default Ubuntu 20.04 repository and can be installed using the apt package manager.

If you have questions, feel free to leave a comment.

Related posts

How to use a password manager

Howto

How to Schedule a Zoom Meeting

Howto

How to watch live broadcast Realme X50 Pro 5G

Howto

How to Send and Receive Fax Online without a Fax Machine or Telephone Line

Howto

Enable Game Preferred Mode on Google Wifi or Nest Wifi for Smoother Streaming Stream

Howto

How to Share Video with Sound when Zooming, the Microsoft Team, and Google Meet

Howto

How to secure your smart home

Howto

How To Use The Nintendo Switch Pro Controller With A Pc

Howto

How to Use Multiple Email Signatures in Gmail

Howto