How to Install and Switch Java Versions on Ubuntu Linux

Java is a powerful programming language and computing platform first released by Sun Microsystems in 1995. Over the years, it has become indispensable for developing everything from mobile applications to large-scale enterprise systems. Installing Java on Ubuntu, a popular Linux distribution, is a straightforward process that can be accomplished through various methods. This tutorial aims to guide you through the installation of Java on Ubuntu, covering both the installation from the Ubuntu repository and the official Oracle package.

In this tutorial you will learn:

  • How to install Java from the Ubuntu repository
  • How to install Java from the official Oracle package
  • How to switch between different Java versions
How to Install and Switch Java Versions on Ubuntu Linux
How to Install and Switch Java Versions on Ubuntu Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux distribution. Used system 24.04 Noble Numbat.
Software OpenJDK or Oracle JDK
Other Internet connection for downloading packages
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Installing Java from the Ubuntu Repository

This method involves installing Java directly from Ubuntu’s repository, which is the simplest and most straightforward method for most users.

  1. Search for Available Java Versions: Begin by searching for all available Java versions in the Ubuntu repository.
    $ apt search openjdk | grep -E 'openjdk-.*-jdk/'

    This command lists all available Java versions for installation. Choose the version that suits your needs.

    HEADLESS VS. FULL JDK EXPLAINED
    The openjdk-jdk-headless package is designed for environments that don’t require a graphical user interface (GUI), making it lighter and more suitable for servers. On the other hand, the openjdk-jdk package includes GUI support for applications that need graphical interaction.
  2. Install Java: Install the chosen version of Java. For example, to install OpenJDK 23:
    $ sudo apt install openjdk-23-jdk

    You can install other versions if needed. We will later discuss how to switch between different Java versions.

  3. Check the Installation: Verify that Java has been installed correctly by checking the version of both the Java runtime and the compiler.
    $ java --version
    $ javac --version

    If the installation was successful, you should see the version of Java you installed displayed in the terminal.

    Check the Java Installation
    Check the Java Installation

Installing Java from the Official Oracle Package

For those who prefer or require the Oracle version of Java, this method involves downloading and installing the package directly from Oracle’s website.

  1. Download the Oracle Java Package: Navigate to Oracle’s Java downloads page at https://www.oracle.com/java/technologies/downloads/ and download the package suitable for your system, likely the x64 Debian package.

    Download the Oracle Java Package
    Download the Oracle Java Package
  2. Install the Downloaded Package: Open a terminal and, from the directory where you downloaded the Oracle Java package, run the following command:
    $ sudo dpkg -i jdk-22_linux-x64_bin.deb

    Replace “22” with the version number of your downloaded package.

  3. Check the Installation: Ensure Java has been installed correctly by checking the version.
    $ java --version
    $ javac --version

    If the installation was successful, the terminal will display the version of Oracle Java you installed.

    version of Oracle Java you installed
    version of Oracle Java you installed



Switching Between Java Versions

JAVA VS. JAVAC: KEY DIFFERENCES
java is the command used to run Java applications, essentially launching the Java Virtual Machine (JVM) with specified class files. On the other hand, javac is the Java Compiler command, used to convert Java source code into bytecode that the JVM can execute. While java brings your applications to life, javac is the tool that transforms your code into a format the machine understands.

If you have installed multiple Java versions from various sources, you may need to switch between them. Here’s how:

  1. For the Java Runtime:
    $ sudo update-alternatives --config java
  2. For the Java Compiler:
    $ sudo update-alternatives --config javac
  3. After running the above commands, select the version of Java you wish to use. You can verify the currently set Java version by checking the version of both the Java runtime and compiler:
    $ java --version
    $ javac --version

    Switching Between Java Versions
    Switching Between Java Versions
UNDERSTANDING ‘AUTO’ VS. ‘MANUAL’ IN UPDATE-ALTERNATIVES
In the context of update-alternatives, the system used to manage default commands in Linux, “auto” mode allows the system to automatically select the highest priority alternative for a given command. This ensures that, without manual intervention, the system uses the most appropriate version of software based on preset priorities. Conversely, “manual” mode gives users the flexibility to manually override this automatic selection, choosing a specific alternative regardless of its priority. This is particularly useful for developers or administrators who need to enforce the use of a specific version of a tool for compatibility or testing purposes.

Conclusion

By following the steps outlined in this tutorial, you should now have Java installed on your Ubuntu system, either from the Ubuntu repository or directly from Oracle. Additionally, you’ve learned how to switch between different Java versions, allowing you to work on various projects that may require different Java environments. Remember, it’s essential to keep your Java installation updated to benefit from the latest features and security patches.

Frequently Asked Questions About Managing Java on Ubuntu

  1. What is the difference between JDK and JRE?

    The JDK (Java Development Kit) includes tools for developing Java applications, notably the compiler and debugger, while the JRE (Java Runtime Environment) contains what is needed to run Java applications but not develop them.

  2. Can I have multiple Java versions installed on Ubuntu?

    Yes, you can install multiple Java versions on Ubuntu. Use the update-alternatives tool to manage and switch between them.

  3. How do I set JAVA_HOME on Ubuntu?

    To set JAVA_HOME, edit your ~/.bashrc or /etc/environment file to include ‘export JAVA_HOME=”/path/to/java”‘, replacing the path with your Java installation directory.

  4. Is OpenJDK free to use for commercial purposes?

    Yes, OpenJDK is open source and free to use for commercial purposes under the GNU General Public License, version 2.

  5. How can I uninstall Java from Ubuntu?

    Use the command ‘sudo apt-get remove openjdk-*’ for OpenJDK or ‘sudo dpkg -r package_name’ for Oracle JDK, replacing ‘package_name’ with the actual package name of your Oracle JDK installation.

  6. What are the security implications of using an outdated Java version?

    Using an outdated Java version can expose your system to security vulnerabilities that have been fixed in later versions, potentially putting your system and data at risk.

  7. How do I update Java on Ubuntu?

    To update Java, use the command ‘sudo apt-get update’ followed by ‘sudo apt-get install java-package’, where ‘java-package’ is the package name of the Java version you wish to install.

  8. Can I install Oracle Java and OpenJDK on the same Ubuntu system?

    Yes, you can install both Oracle Java and OpenJDK on the same system and use update-alternatives to switch between them.

  9. What is the command to list all installed Java versions on Ubuntu?

    Use ‘update-java-alternatives -l’ to list all installed Java versions and their paths.

  10. How do I choose the default Java compiler for my projects?

    Use the ‘update-alternatives –config javac’ command to select the default Java compiler from the installed versions.

  11. Why would I need the headless version of the JDK?

    The headless version is useful for server environments where GUI functionality is not needed, reducing the resource footprint.

  12. How does Ubuntu choose the default Java version when multiple versions are installed?

    Ubuntu uses the priority system in update-alternatives to choose the default version. The version with the highest priority becomes the default unless manually overridden.

  13. How do I manually override the default Java version?

    Use ‘sudo update-alternatives –config java’ to manually select the default Java runtime, and ‘sudo update-alternatives –config javac’ for the compiler.

  14. What should I do if a Java application doesn’t run with the current default Java version?

    Try switching to a different Java version using update-alternatives, or check the application documentation for the required Java version.

  15. Is there a performance difference between OpenJDK and Oracle JDK?

    Performance differences between OpenJDK and Oracle JDK are minimal for most applications, but specific features or optimizations in Oracle JDK may benefit certain applications.



Comments and Discussions
Linux Forum