How to Install Java on Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04

Java is one of the most popular programming languages and the JVM (Java’s virtual machine) is the run-time environment to run Java applications. These two platforms are required for many popular software that includes Tomcat, Jetty, Cassandra, Glassfish, and Jenkins.

This tutorial will guide you through the installation of Java on Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04, covering both the installation from the Ubuntu repository and the official Oracle package.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

Installing Java from the Ubuntu Repository

To begin, search for all available Java versions in the Ubuntu package repository.

apt search openjdk | grep -E 'openjdk-.*-jdk/'
Search Java Versions in Ubuntu
Search Java Versions in Ubuntu

Next, choose the Java version that suits your needs. Here we are installing openjdk-21-jdk (Java Development Kit), which includes the compiler, libraries, and tools needed for developing Java applications.

sudo apt install openjdk-21-jdk
Install Java in Ubuntu
Install Java in Ubuntu

Verify that Java has been installed correctly by checking the version of both the Java runtime and the compiler.

java --version
javac --version
Check Java in Ubuntu
Check Java in Ubuntu

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

Installing Java from the Official Oracle Package

Download the Oracle Java package from the official Oracle website or use the following wget command to download directly in the terminal.

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

Once downloaded, use the following dpkg command to install the Oracle Java installer file that includes the necessary binaries and files to run the Java Development Kit on your system.

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

Verify that Java has been installed correctly by checking the version of both the Java runtime and the compiler.

java --version
javac --version
Verify Java in Ubuntu
Verify Java in Ubuntu

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

Managing Multiple Java Versions in Ubuntu

Having multiple versions of Java installed allows you to test and develop applications using different versions of the Java platform.

You can install multiple versions of Java from the Ubuntu repository and switch between different Java versions using the update-alternatives command as shown.

List all available Java versions installed on your system.

update-alternatives --list java

To switch between different Java versions, use the following command.

sudo update-alternatives --config java
Multiple Java Versions in Ubuntu
Multiple Java Versions in Ubuntu
Conclusion

Installing Java on Ubuntu 24.04, Ubuntu 22.04, and Ubuntu 20.04 is a straightforward process that can be accomplished through various methods. This tutorial has covered both the installation from the Ubuntu repository and the official Oracle package.

Additionally, it has discussed how to switch between different Java versions. By following these steps, you can successfully install and manage Java on your Ubuntu system.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

3 Comments

Leave a Reply
  1. Hi everyone,

    Ubuntu 24.04 no longer supports running .jar files directly. This option is no longer available in the graphical interface, nor is it possible to run Java programs from the command line.

    Java is installed correctly, as confirmed by running the javac -version and java -version commands.

    Reply
    • @Luis,

      It sounds like there might be an issue with the Java setup on your system. Even though Java is installed, sometimes Ubuntu requires additional configurations to recognize .jar files.

      Here are a few troubleshooting steps you could try:

      1. Run sudo update-alternatives --config java to ensure the correct Java version is set as the default.

      2. Use java -jar /path/to/yourfile.jar in the terminal to launch the .jar file. This should work if Java is properly configured.

      3. If you haven’t already, try installing OpenJDK (e.g., sudo apt install openjdk-17-jdk) to make sure Ubuntu has all the necessary Java components.

      Let me know if this helps, or if you’re seeing any specific error messages!”

      Reply
  2. Hello,

    I am having issue. After adding the line JAVA_Home.

    $ source /etc/environment
    

    bash: /etc/environment: line 2: unexpected EOF while looking for matching `”‘

    bash: /etc/environment: line 3: syntax error: unexpected end of file

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.