A Comprehensive Guide to Setting Up Java on Ubuntu

Published December 19, 2023

Lets get ready for a new ride to jump in.

“A Comprehensive Guide to Setting Up Java on Ubuntu”.

Java

Explore More, Setting up Plex on Ubuntu for Media Storage

Java ,a versatile and object-oriented programming language, has been meticulously crafted to transcend platform limitations.

Its remarkable feature of platform independence allows Java programs to seamlessly run on any device or operating system equipped with the Java Virtual Machine (JVM).

The ubiquity of Java in the developer community is undeniable, as it is widely embraced for its exceptional capabilities in software development.

Moreover, Java’s influence extends beyond its programming prowess. Esteemed software applications like Tomcat, Glassfish, and Jetty heavily rely on the robust foundation provided by this renowned programming language.

Consequently, Java emerges as an indispensable and pragmatic component in the realm of software development and execution.  Notably, Java’s compatibility with the esteemed Ubuntu operating system further solidifies its significance. This harmonious alliance has made Java an optimal choice for a myriad of software development projects on the Ubuntu platform.

In light of this, we have curated a comprehensive guide to assist developers utilizing the Ubuntu system in seamlessly installing Java.  Indulge in the seamless integration of Java and Ubuntu, and unlock a world of limitless possibilities in software development.

Essential Requirements for Java Installation on Ubuntu

Choose Java for Server-Side Development, as it powers many organizational applications and web services. Optimize your experience by selecting Ubuntu, a popular Linux distribution for server environments. Ubuntu’s seamless compatibility with the standard Java version (OpenJDK) simplifies the installation process.

Consider investing in an Ubuntu VPS with Ubuntu 22.04 for a comprehensive solution that addresses all your requirements.

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.

Explore the evolving landscape of Java platforms and versions by visiting the official websites of Oracle or OpenJDK for the latest releases. Ubuntu’s default repository offers two key packages: OpenJDK for Java development and programming, and Open JRE for running Java-based applications. Easily install these packages. Note that the official Ubuntu repository lacks Oracle Java; for this version, utilize a third-party repository during installation.

Comprehensive Guide to Install Java on Ubuntu

Ubuntu Linux seamlessly integrates with the standard Java version, making installation straightforward through the apt package manager. Access the main Java packages, OpenJDK, and Open JRE, from the official Ubuntu repository. For Oracle Java, opt for installation via a third-party repository or by downloading the .deb file. Tailor your Java setup based on specific needs or install multiple versions using the instructions outlined in this article. Configure your Ubuntu system to use the default version.

Let’s take a quick look at the steps for installing Java on 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

Prior to installing Java on Ubuntu, verify if it’s necessary by checking for existing installations. Run the following command to ensure that Java is not already present on your Ubuntu system:

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]

The output indicates that Java is not available on the Ubuntu system. It’s now time to proceed with the installation 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

Confirm the installation by typing ‘Y’ and pressing Enter. The installation process may take some time, but it will be successful eventually.

If you have specific requirements, you can install a particular version of OpenJDK on Ubuntu.

For instance, to install OpenJDK 11, type:

sudo apt install openjdk-11-jdk

For installing a different OpenJDK version, substitute the desired version number (e.g., 8, 13, 16, etc.) for 11.

Transitioning to JDK, it serves as a platform furnishing crucial tools for Java development, including compilers and debuggers, complementing the features of JRE.

  • Install Oracle JDK

Unlike the packaged OpenJDK and OpenJRE 11 within Ubuntu, Oracle JDK is absent from the official Ubuntu repository due to Oracle’s licensing agreement restrictions. Installing Oracle JDK, the officially distributed version by Oracle, can be done through two methods:

1. Utilizing a third-party repository, or

2. Manually installing by downloading the .deb file.”

Download and Install Oracle JDK Using 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

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, replace 17 with the desired Java version in the previous command.

While installing Oracle Java, press the Tab key followed by Enter to accept the license agreement terms.

Next, the Java configuration wizard will ask you to confirm the Oracle license terms once again.

Confirm by pressing the ‘Tab’ key and then ‘Enter’. Successfully accepting the terms completes the Java installation.

To designate Oracle Java as the default version, use the following command:

sudo apt install oraclejava17setdefault

 

By installing Oracle JDK, you not only gain the Java Development Kit but also incorporate the features of JRE.

Consequently, you’ve acquired the knowledge of installing the Java version suitable for your requirements on Ubuntu.

The decision between Java versions hinges on selecting the right licensing for your specific circumstances.

4. Verify Installation

After finishing the installation of Java versions, you can confirm the correct installation by executing 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 can install multiple Java versions on your Ubuntu server, allowing you to cater to various objectives. If you’ve installed multiple versions for different purposes, learn how to manage them effectively on your Ubuntu system.

Begin by using 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

In our sample output, we’ve installed three Java versions, and one version is denoted with an asterisk (*), signifying it as the default.

To switch to a different default version, enter the number of the desired version and press Enter.

6. Configuring JAVA_HOME Environment Variable

In simple terms, the JAVA_HOME environment points to the directory where Java is installed. Configuring this variable becomes crucial when establishing and running Java-based software, as Java programs rely on information about the JAVA_HOME directory during setup.

To set up the JAVA_HOME environment variable, start by locating the Java installation path.

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

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 conclusion of the file, paste the copied path for the Java version, while excluding the ‘/bin’ portion from 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

By showcasing the Java installation path, you can confirm that the environment variable has been configured correctly.

Uninstalling Java in Ubuntu

For any reason, if you wish to remove the Java package from your Ubuntu system, utilize the apt remove command.

To uninstall the default version of OpenJDK, execute 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 preceding command, a prompt will appear, asking for confirmation to remove Java. Press ‘y’ to proceed with the removal of the Java package.

Explore more:Installing npm and Node.js on Ubuntu Server
Stay Connected on Meta, X, Instagram .