How to install Java in Ubuntu

Installing Java in Ubuntu: A Comprehensive Setup Guide

Java is a versatile and object-oriented programming language designed to be platform-independent. This means that Java programs are compatible with any device and operating system with the Java Virtual Machine (JVM). Any developer is undoubtedly familiar with the Java programming language and employs it for development purposes. Additionally, a wide range of software, such as Tomcat, Glassfish, and Jetty, relies on this popular programming language.

Therefore, Java can be considered one of the essential and practical elements for software development and execution, offering numerous advantages compared to other programming languages. Java’s compatibility with Ubuntu has made it a good choice for various software development projects on the Ubuntu operating system. For this reason, in this guide, we will instruct developers using the Ubuntu system on how to install Java in Ubuntu.

Java has developed various versions for different development purposes. In this article, you will learn how to install the main versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) in Ubuntu using the apt package manager. To take advantage of Java’s key features on Ubuntu, read this article to the end.

Prerequisites for Installing Java on Ubuntu

Java is commonly a good choice for Server-Side Development, and most organizational-level applications and web services run on Java-based frameworks. On the other hand, Ubuntu is one of the popular Linux distributions often preferred by users for server environments. Additionally, Ubuntu’s default compatibility with the standard Java version (OpenJDK) facilitates the use of Java on Ubuntu. Therefore, it can be concluded that for optimal usage of Java and easy installation on Ubuntu, buying Ubuntu VPS with Ubuntu 22.04 is a smart move to address all aspects of your needs.

Furthermore, to install Java in Ubuntu, you need a user account with sudo privileges and access to the terminal.

Java Versions for Installation on Ubuntu

It is not without reason that we have introduced Java as a powerful and versatile programming language because it encompasses various platforms designed for different purposes. Here, we will refer to them:

Standard Edition (Java SE): This version is developed to provide the core functionality of the Java programming language. It includes Java Standard Edition API (java.lang, java.util, etc.) and core libraries. This version is used for developing server-side applications, command-line tools, and desktop applications.

Enterprise Edition (Java EE): Java EE is designed to run on software servers and enterprise-level software. It supports large-scale, multi-tiered, and scalable network applications. It provides features such as Java Servlets, JavaServer Pages (JSP), Enterprise JavaBeans (EJB), Java Message Service (JMS), and more.

Micro Edition (Java ME): This version targets devices with limited resources, such as mobile phones and small-scale devices, covering mobile development goals. It offers a subset of Java SE to adapt to the constraints of these devices.

JavaFX: JavaFX provides a platform for building Rich Internet Applications (RIAs), graphical user interfaces, and multimedia-rich applications.

Oracle Cloud: Oracle Cloud offers a platform for running and deploying Java applications in the cloud, incorporating additional commercial features.

Java platforms and versions are evolving, and the official websites of Oracle or OpenJDK assist you in utilizing the latest Java versions. Two Java packages, OpenJDK (suitable for Java development and programming) and Open JRE (ideal for running Java-based applications), are available in the Ubuntu repository by default and can be easily installed. However, the official Ubuntu repository does not provide the Oracle Java version, so you must use a third-party repository to install Oracle Java.

Comprehensive Guide to Install Java on Ubuntu

Since Ubuntu Linux is, by default, compatible with the standard Java version, the simplest method is to install it through the apt package manager. The official Ubuntu repository provides the main Java packages, which are Open JDK and Open JRE. However, you must install the Oracle Java version from a third-party repository or download the .deb file. Depending on your needs, install the appropriate Java version, or you can install all of them using the instructions in this article and then configure the Ubuntu system to use the default version. First, let’s have a quick glance at the steps for Installing Java in Ubuntu:
1. Check whether Java is already installed on Ubuntu.
2. Update the package list.
3. Install the standard Java version (Default JRE/JDK) and the Oracle JDK version.
4. Verify that Java is successfully installed on Ubuntu.
5. Set the default version of Java for use.
6. Configure the JAVA_HOME environment variable.
In the following, you will learn how to install Java in Ubuntu.

1. Check if Java is not already installed in Ubuntu

Before installing Java Ubuntu, first, check whether it is necessary to install it or not. Java might have been previously installed on your Ubuntu system, and there might be no need to install it again. Therefore, run the following command to ensure that your Ubuntu system is not already equipped with Java:

java - version

Output:

Command 'java' not found, but can be installed with:
sudo apt install default-jdk
sudo apt install default-jre
apt install [first version available]
apt install [second version available]

As you can see in the output, Java is unavailable in the Ubuntu system, so now it’s time to install it for development purposes.

2. Update Package List

To use the latest versions and ensure an up-to-date system on your Ubuntu, update the package list using the following command:

sudo apt update

Output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

3. Install Java with apt in Ubuntu

OpenJDK and OpenJRE are the primary open-source options for Java, and they can be installed using the apt package manager.

Ubuntu 22.04, by default, supports OpenJDK 11, which has the same functionality as JRE and JDK. Below, you will learn how to install the main versions of Java.

  • Install JRE from OpenJRE 11

Run the following command to download and install JRE from OpenJDK 11:

sudo apt install default-jre

To confirm the installation, type ‘y‘ and then press Enter. This version of Java is intended for running Java-based applications and does not provide additional features for development and compiling source code.

  • Install OpenJDK

Install the latest version of the Java Development Kit (JDK) on Ubuntu by running the following command:

sudo apt install default-jdk

To confirm the installation, type ‘Y‘ and press Enter. The installation may take some time, but it will be successful after a while.

You can install a specific version of OpenJDK in Ubuntu based on your needs. For example, to install OpenJDK 11, type:

sudo apt install openjdk-11-jdk

To install another version of OpenJDK, replace the desired version number (e.g., 8, 13, 16, etc.) with 11.

JDK is a platform that provides essential tools for Java development, such as compilers and debuggers, complementing the features of JRE.

  • Install Oracle JDK

Unlike OpenJDK and OpenJRE 11, which are packaged with Ubuntu, Oracle JDK is not available in the official Ubuntu repository because Oracle’s licensing agreement for Java does not permit installation through the Ubuntu official repository. There are two ways to install Oracle JDK, which is the officially distributed version by Oracle: 1. Through a third-party repository, 2. Manual installation by downloading the .deb file.

In the following, you will learn how to install Oracle JDK using both methods.

Install Oracle JDK by downloading the Deb File:

Visit the Oracle JDK download page and download the version you need. Typically, the latest version addresses your requirements, so choose the latest version based on the software installation time.

Then, select the appropriate package. You can download the .deb files from the official Oracle website using a web browser and install them, or use the command-line tool wget for downloading and installing.

First, install the necessary dependencies by executing the following command in Linux Terminal:

sudo apt install libc6-i386 libc6-x32 libxi6 libxtst6 -y

Then use the following command to download the Java 17 .deb package:

wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb

You can replace the version specified in the previous command with the one that suits your needs.

After downloading the Java 17 .deb package, install it by running the following command:

sudo dpkg -i jdk-17_linux-x64_bin.deb

Output:

Selecting previously unselected package jdk-17.
(Reading database … 206101 files and directories currently installed.)
Preparing to unpack jdk-17_linux-x64_bin.deb …
Unpacking  jdk-17 (17.0.8-ga) …
Setting up jdk (17.0.8-ga)…

Install Oracle JDK through Third-Party Repository:

Installing Oracle JDK via a Third-Party Repository begins with installing the required dependencies:

sudo apt install software-properties-common

Then, you need to add the repository to the list of package sources:

sudo add-apt-repository ppa:linuxuprising/java -y

To make the repository you recently added to your Ubuntu system available, update the package list:

sudo apt update

Finally, you can install Oracle Java 17 from the third-party repository you added to your Ubuntu system using the apt package manager:

sudo apt install oracle-java17-installer

You can install a different version of Oracle Java from the third-party repository instead of Java 17; for this purpose, use the following syntax:

sudo apt install oracle-java[Java version number]-installer

As a result, in the previous command, you can replace 17 with the version number of the Java you desire.

During the Oracle Java installation process, press the Tab key and then Enter to accept the license agreement terms.

Subsequently, the Java configuration wizard will prompt you to confirm the Oracle license terms again. Once more, confirm the request by pressing the “Tab” key and then “Enter”. After accepting the terms, Java will be successfully installed.

To set Oracle Java as the default version, execute the following command:

sudo apt install oracle-java17-set-default

Oracle JDK includes the JRE option, so by installing Oracle JDK, you will also benefit from the features of JRE.

As a result, you have now learned how to install the appropriate version of Java on Ubuntu for your needs. Choosing between Java versions depends on the proper licensing for your circumstances.

4. Verify Installation

After completing the installation of Java versions, you can verify that Java has been installed correctly by running the following command:

java -version

You can ensure that Java has been successfully installed in Ubuntu by receiving the information about the installed Java version.

5. Manage Java versions on Ubuntu

Fortunately, you have the ability to install multiple different versions of Java on your Ubuntu server. Therefore, if you have installed multiple versions on your Ubuntu server to cover various objectives, you need to know how to manage multiple Java versions on your Ubuntu system. You can select the default Java version on your Ubuntu system. To do this, first, use the following command to view the installed Java versions on your Ubuntu server:

sudo update-alternatives --config java

Sample 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-17-oracle/bin/java          1091             manual mode
3           /usr/lib/jvm/jdk-17-oracle-x64/bin/java      285278208          auto mode
Press <enter> to keep the current choice[*], or type selection number:

In our sample output, three Java versions are installed, and one version is marked with an asterisk (*), indicating it is set as the default version. To change the default version, type the number of the desired version and press Enter.

6. Configuring JAVA_HOME Environment Variable

The JAVA_HOME environment, in simple terms, points to the directory where Java is installed. Configuring the JAVA_HOME environment variable is useful when setting up and running Java-based software because Java programs need information about the JAVA_HOME directory during setup.

To configure the JAVA_HOME environment variable, you first need to locate the Java installation path. To do this, execute the following command:

sudo update-alternatives --config java

Sample 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-17-oracle/bin/java          1091           manual mode
3           /usr/lib/jvm/jdk-17-oracle-x64/bin/java       285278208      auto mode
Press <enter> to keep the current choice[*], or type selection number:

As you can see, the previous command displays the installation path of Java in the ‘path’ section.

Copy the relevant path for your desired Java version, then open the /etc/environment file with your preferred text editor:

sudo nano /etc/environment

At the end of the file, paste the copied path for the Java version, but remove the ‘/bin‘ portion of the path, as follows:

JAVA_HOME="/your/installation/path/"

For example:

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

After adding the Java installation path to the file, save your changes in the editor and exit. As a result, you have configured the JAVA_HOME path on your Ubuntu system.

To apply the changes in your current session, reload the /etc/environment file:

source /etc/environment

To confirm the environment variable is configured, run the following command:

echo $JAVA_HOME

Displaying the Java installation path ensures that you have configured the environment variable correctly.

Uninstalling Java in Ubuntu

If, for any reason, you want to remove the Java package from your Ubuntu system, you can use the apt remove command. If you want to uninstall the default version of OpenJDK, run the following command:

sudo apt remove default-jdk

To uninstall the specific version of OpenJDK, use the below Syntax:

sudo apt remove openjdk-[Java version number]-jdk

After executing the previous command, you will be prompted to confirm the removal of Java. Therefore, you need to press ‘y‘ to remove the Java package.

Conclusion

Java is a popular and versatile programming language that caters to the needs of software developers. The standard version of Java (OpenJDK) is widely used on Ubuntu and most Linux distributions, making it easier to use and run Java-based software at an organizational level, such as Jenkins, Jetty, Glassfish, and more. Installing Java on Ubuntu servers is an Intelligent and beneficial move for development in server environments.

While a broad spectrum of programming languages is available today, Java is extensively used in server and desktop environments due to its valuable features.

Community support and the availability of resources and assistance in the Ubuntu and Java communities have made the Java programming language a popular choice for developing various software projects on Ubuntu.

This article taught you how to install Java in Ubuntu, manage Java versions, and configure the JAVA_HOME environment variable. Now, the platform for developing and running Java software on Ubuntu is prepared for you to enjoy.

If you need further guidance on installing Java on Ubuntu, let us know.

Leave a Reply

Your email address will not be published. Required fields are marked.