How to Install and Manage Java on Ubuntu: OpenJDK vs Oracle JDK, Setup, and Troubleshooting

目次

1. What Is Java? When Do You Need Java on Ubuntu?

What Kind of Programming Language Is Java?

Java is an object-oriented programming language that is widely used around the world. One of its core design principles is “Write Once, Run Anywhere,” meaning that applications can run on different operating systems and devices without modification.

Java is used in a wide range of development scenarios, including desktop applications, web applications, and mobile apps (especially Android development). It is also heavily used in large-scale enterprise systems and financial systems, making it one of the most enduring and actively used programming languages today.

The Role of Java on Ubuntu

Java plays an important role even on Linux-based operating systems like Ubuntu. Java is required in the following situations:

1. Running Java-Based Applications

Many applications are developed in Java and require a Java Runtime Environment (JRE) to run. For example, development tools such as Eclipse and IntelliJ IDEA, as well as database management tools like DBeaver, will not start without Java.

2. Building a Java Development Environment

To learn Java programming or develop Java applications on Ubuntu, installing the Java Development Kit (JDK) is essential. Ubuntu is popular as a development platform and works very well with Java, making this combination widely used by developers.

3. Running Server Applications

Java is the foundation of many web servers and frameworks such as Tomcat and Spring Boot. When running Java-based web applications on an Ubuntu server, a Java environment is mandatory.

Summary: Ubuntu and Java Are a Perfect Match

Because Ubuntu is open-source and lightweight, it pairs extremely well with cross-platform languages like Java. Installing Java on Ubuntu enables a wide range of use cases, from application usage to development and server operations.

2. Java Options on Ubuntu (OpenJDK vs Oracle JDK)

Available Java Choices on Ubuntu

On Ubuntu, there are two main Java development environments available: OpenJDK and Oracle JDK.

Both can be used to run and develop Java programs, but they differ in terms of licensing, distribution, and support. Choosing the right JDK depends on your purpose and use case.

What Is OpenJDK? (Ubuntu’s Default JDK)

OpenJDK (Open Java Development Kit) is the open-source implementation of Java. Originally led by Sun Microsystems (now Oracle), it continues to evolve through open community development.

Ubuntu adopts OpenJDK as its default Java environment, and it can be easily installed using the apt command.

Main Features of OpenJDK

  • Completely free, including commercial use
  • Easy installation from Ubuntu’s official repositories
  • Regular security updates
  • Highly stable and widely used in enterprise environments

If you do not have special requirements, OpenJDK is usually the best and safest choice.

What Is Oracle JDK? (Officially Supported JDK)

Oracle JDK is the official Java implementation provided by Oracle. It is based on OpenJDK but may include additional optimizations or integrations.

However, its license differs, and commercial use may require a paid license. Personal or educational use is generally permitted.

Main Features of Oracle JDK

  • Official Oracle support and maintenance
  • License confirmation required for commercial use
  • Fast delivery of security patches
  • Includes some additional features not found in OpenJDK

Installation on Ubuntu requires manual setup by downloading it from the official website.

Summary of Differences Between OpenJDK and Oracle JDK

CategoryOpenJDKOracle JDK
ProviderOpen-source communityOracle Corporation
LicenseGPL + Classpath ExceptionOracle Binary Code License (commercial use requires caution)
CostFreePaid for some commercial use cases
Typical UseGeneral development, learning, internal toolsCommercial services, large-scale operations
InstallationEasy via aptManual download and setup

Conclusion: OpenJDK Is Recommended for Most Ubuntu Users

For most users, OpenJDK is more than sufficient when setting up Java on Ubuntu. It is reliable and compatible with most frameworks and libraries.

If you require official support or enterprise-level guarantees, Oracle JDK may be an option. Choose the JDK that best fits your needs.

3. Preparation Before Installing Java on Ubuntu

Things to Check Before Installation

Installing Java on Ubuntu is not difficult, but a few preparation steps will help ensure a smooth process. This is especially important for beginners or systems with many existing packages.

1. Update the Package List

Ubuntu uses APT (Advanced Package Tool) to manage software. If the package list is outdated, Java packages may not be found or may be outdated.

Update the package list with the following command:

sudo apt update

This ensures that APT installs Java using the latest available package information.

2. Check for Existing Java Installations

Older Java packages may conflict with new installations.

Check the current Java version:

java -version

If a version is displayed, Java may already be installed. Uninstalling it before reinstalling can help avoid issues.

3. Confirm Administrator (sudo) Privileges

Installing Java requires system-level changes, so sudo privileges are required.

If permissions are missing, you may see an error like:

User 'yourname' is not in the sudoers file. This incident will be reported.

In that case, contact an administrator or use a user with root privileges.

Summary

Updating packages and checking your environment in advance helps prevent installation errors and ensures a smooth setup process.

4. How to Install Java on Ubuntu (Basic Methods)

4.1 Installing OpenJDK Using APT

Ubuntu uses APT (Advanced Package Tool) for software management. Since OpenJDK is included in Ubuntu’s official repositories, it can be installed easily using the following commands.

Install OpenJDK 11 (Stable LTS)

sudo apt install openjdk-11-jdk

Install OpenJDK 17 (Newer LTS)

sudo apt install openjdk-17-jdk

Replace the XX part of openjdk-XX-jdk with the desired version. As of April 2025, 11 and 17 are recommended Long-Term Support (LTS) versions.

APT automatically resolves dependencies, making installation safe and easy even for beginners.

4.2 Verify the Installed Java Version

After installation, verify that Java is working correctly by running:

java -version

Example output (OpenJDK 17):

openjdk version "17.0.9" 2024-10-17
OpenJDK Runtime Environment (build 17.0.9+8-Ubuntu)
OpenJDK 64-Bit Server VM (build 17.0.9+8-Ubuntu, mixed mode, sharing)

If the version information is displayed, Java has been installed successfully.

4.3 Notes When Multiple Java Versions Are Installed

Ubuntu allows multiple Java versions to be installed simultaneously, but only one version can be used as the default.

If multiple versions are installed, select the active version using:

sudo update-alternatives --config java

You can switch versions by selecting the corresponding number.

Summary: Start with OpenJDK

For most users, installing OpenJDK 11 or 17 is the best way to get started with Java on Ubuntu. With APT, a complete Java environment can be set up in minutes.

5. How to Set JAVA_HOME (Environment Variable Configuration)

What Is JAVA_HOME?

JAVA_HOME is an environment variable that tells applications and tools where Java is installed.
Many build tools such as Maven and Gradle rely on this variable to locate Java.

This variable is not set automatically, so it must be configured manually.

How to Find the JAVA_HOME Path

Check the actual path of the currently used Java executable:

readlink -f $(which java)

Example output:

/usr/lib/jvm/java-17-openjdk-amd64/bin/java

The JAVA_HOME path is the directory without bin/java:

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

Steps to Set JAVA_HOME

Edit your shell configuration file. On Ubuntu, this is usually ~/.bashrc (or ~/.zshrc for Zsh).

1. Edit the bashrc File

nano ~/.bashrc

Add the following lines at the end (adjust the path if needed):

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH

2. Apply the Changes

source ~/.bashrc

Verify the Configuration

echo $JAVA_HOME

If the correct path is displayed, the configuration is complete.

Note: Switching JAVA_HOME When Changing Versions

When switching Java versions, update JAVA_HOME accordingly to match the selected version.

Summary

Setting JAVA_HOME is essential for many Java-based tools. Although it requires manual setup, it prevents future configuration issues.

6. Managing and Switching Multiple Java Versions

Why Use Multiple Java Versions?

Some applications require specific Java versions. For example, one project may require Java 8, while another requires Java 17.

Ubuntu allows multiple Java versions to coexist and be switched easily.

How to Switch Java Versions

Step 1: Install Multiple Versions

sudo apt install openjdk-8-jdk
sudo apt install openjdk-17-jdk

Step 2: Select the Java Version

sudo update-alternatives --config java

Switch the Java Compiler (javac)

sudo update-alternatives --config javac

Update JAVA_HOME If Necessary

After switching versions, ensure JAVA_HOME matches the active Java version.

Common Mistakes

  • Java switched but not reflected in the IDE:
    Update the Java path in the IDE settings.
  • java switched but javac did not:
    Run update-alternatives for javac separately.

Summary

Ubuntu makes it easy to manage multiple Java versions, which is especially useful when working on multiple projects.

7. Common Errors and How to Fix Them

Error 1: “Unable to locate package openjdk-xx-jdk”

Symptoms

E: Unable to locate package openjdk-17-jdk

Causes

  • Outdated package list
  • Typographical errors
  • Unsupported Java version for your Ubuntu release

Solution

sudo apt update
apt search openjdk

Error 2: “java: command not found”

Solution

sudo apt install default-jre

Error 3: JAVA_HOME Not Applied

source ~/.bashrc
echo $JAVA_HOME

Error 4: javac Not Found

sudo apt install openjdk-17-jdk
javac -version

Error 5: Java Version Does Not Change

sudo update-alternatives --config java
sudo update-alternatives --config javac

Summary

Most Java-related issues on Ubuntu are caused by configuration mistakes. Read error messages carefully and address them step by step.

8. Installing Java Using a GUI (Beginner-Friendly)

No Need to Fear the Terminal

Ubuntu provides a graphical interface that allows software installation using only the mouse.

Steps Using Ubuntu Software

Step 1: Open Ubuntu Software

Step 2: Search for “OpenJDK”

Step 3: Select a Version and Install

Post-Installation Verification

java -version

Limitations of GUI Installation

GUI installation offers limited version control. Advanced users should use the terminal.

Summary

GUI installation is suitable for beginners, while terminal-based installation offers more flexibility.

9. Java Development Environment Setup (Optional)

Required Tools

ToolPurpose
JDKJava development and execution
IDECode editing, debugging, completion
Build ToolsDependency management and builds

Recommended IDEs

Eclipse

sudo snap install eclipse --classic

IntelliJ IDEA

sudo snap install intellij-idea-community --classic

Visual Studio Code

sudo snap install code --classic

Build Tools

sudo apt install maven
sudo apt install gradle

Summary

Ubuntu provides a stable and powerful Java development environment suitable for beginners and professionals alike.

10. Frequently Asked Questions (FAQ)

Q1. Which should I choose: OpenJDK or Oracle JDK?

In most cases, OpenJDK is sufficient. Oracle JDK is suitable when official support is required.

Q2. Do I need to set JAVA_HOME?

It is optional for running Java but recommended for development and build tools.

Q3. How do I uninstall Java?

sudo apt remove openjdk-17-jdk

Q4. GUI or Terminal?

GUI is fine for basic use. The terminal is recommended for advanced configuration.

Summary

This guide helps you install, configure, and manage Java on Ubuntu with confidence.