How to Install OpenJDK 8 on Ubuntu (26.04, 24.04, 22.04)

OpenJDK 8 is a free, open-source implementation of the Java Platform used primarily to run and develop legacy Java applications. Despite being an older Long-Term Support release, OpenJDK 8 remains essential for enterprise software, build tools, and applications that require Java 8 compatibility. By the end of this guide, you will have OpenJDK 8 installed on Ubuntu using the default APT repository, with the ability to manage multiple Java versions and verify your installation.

Compare OpenJDK LTS Releases for Ubuntu

Before installing, confirm that OpenJDK 8 matches your project requirements. The table below compares LTS Java versions available in Ubuntu’s default repositories.

Java VersionLTS Support UntilChoose It WhenTrade-offs
OpenJDK 8December 2030 (Adoptium)Legacy applications, older frameworks like Spring 4.x, or vendor-certified deployments requiring Java 8Missing modern language features (no var, no records, no virtual threads); some libraries dropping Java 8 support
OpenJDK 11October 2027 (Adoptium)Enterprise applications, build servers, projects needing stable LTS with module system support and modern TLSLacks features from Java 17+; some newer frameworks require 17+ as minimum
OpenJDK 17October 2027 (Adoptium)Spring Boot 3.x, Jakarta EE 10+, or workloads benefiting from records and sealed classesLacks virtual threads and pattern matching from newer releases
OpenJDK 21December 2029 (Adoptium)High-concurrency applications using virtual threads, projects needing structured concurrencySome features still maturing; verify framework compatibility
OpenJDK 25September 2031 (Adoptium)New projects, applications requiring the latest stable LTS features and performance improvementsNewest LTS release; verify framework compatibility before production migration

Choose OpenJDK 8 when your application explicitly requires Java 8 compatibility, your CI/CD pipeline targets this version, or you need to run legacy systems that cannot migrate to newer Java releases. For new projects without legacy constraints, consider starting with OpenJDK 17 or newer.

Update Ubuntu Before OpenJDK 8 Installation

Update your Ubuntu system to ensure all packages are current and avoid potential conflicts during installation:

sudo apt update
sudo apt upgrade

The first command refreshes your package index, while the second upgrades installed packages to their latest versions. Depending on how many packages require updates, this process may take a few minutes.

Install OpenJDK 8 on Ubuntu

Ubuntu’s default repositories include OpenJDK 8 packages, so installation is straightforward without adding external sources. View the available OpenJDK 8 packages with:

apt-cache search openjdk-8

This command displays all packages related to OpenJDK 8:

openjdk-8-doc - OpenJDK Development Kit (JDK) documentation
openjdk-8-jdk - OpenJDK Development Kit (JDK)
openjdk-8-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-8-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-8-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-8-source - OpenJDK Development Kit (JDK) source files

Install JRE or JDK

Choose the package that matches your needs. If you only need to run Java applications, install the Java Runtime Environment (JRE):

sudo apt install openjdk-8-jre

If you plan to develop Java applications or use build tools like Apache Maven, install the full Java Development Kit (JDK). This includes the JRE plus development tools such as the Java compiler:

sudo apt install openjdk-8-jdk

Verify the Installation

After installation completes, verify that Java is accessible by checking the installed version. OpenJDK 8 uses the single-hyphen -version flag, not the double-hyphen --version used by Java 9 and later:

java -version

You should see output confirming OpenJDK 8 is installed:

openjdk version "1.8.0_472"
OpenJDK Runtime Environment (build 1.8.0_472-8u472-ga-1~24.04-b08)
OpenJDK 64-Bit Server VM (build 25.472-b08, mixed mode)

If you installed the JDK, verify the Java compiler is available:

javac -version
javac 1.8.0_472

Manage Multiple Java Versions

If you have multiple Java versions installed on your system, Ubuntu provides the update-alternatives command to switch between them. This feature is useful when different projects require different Java versions.

Switch the Java Runtime

To view all installed Java runtimes and select one as the system default:

sudo update-alternatives --config java

This command displays an interactive menu showing all available Java installations with their paths and priorities:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      2111      auto mode
  1            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  2            /usr/lib/jvm/java-21-openjdk-amd64/bin/java      2111      manual mode

Press <enter> to keep the current choice[*], or type selection number:

Enter the selection number corresponding to OpenJDK 8 (in the example above, type 1) and press Enter. Verify the change by running java -version again.

Switch the Java Compiler

The Java compiler (javac) has its own alternatives configuration. If you installed the JDK and need both java and javac to use the same version, configure the compiler separately:

sudo update-alternatives --config javac

Select the same version number you chose for the Java runtime. Mismatched versions between java and javac cause compilation errors when your compiled bytecode targets a different runtime version.

Set JAVA_HOME Environment Variable

Many Java applications and build tools require the JAVA_HOME environment variable to locate your Java installation. For detailed instructions on configuring this variable persistently, see our guide on setting the Java environment path in Ubuntu.

As a quick reference, find the path to your active Java installation with:

dirname $(dirname $(readlink -f $(which java)))

On 64-bit Ubuntu systems with OpenJDK 8 as the active version, this command returns:

/usr/lib/jvm/java-8-openjdk-amd64/jre

Test the Installation

To confirm that Java compiles and runs correctly, create a simple test program. This step verifies that both the runtime and the compiler are functioning as expected.

Create a Test Program

Create a new Java source file using your preferred text editor:

nano Hello.java

Add the following Java code to the file:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from LinuxCapable!");
    }
}

Save the file by pressing Ctrl+O, then exit with Ctrl+X.

Compile and Run

Compile the Java source code into bytecode:

javac Hello.java

If compilation succeeds without errors, a Hello.class file appears in the current directory. Run the compiled program:

java Hello

Expected output:

Hello from LinuxCapable!

If you see this output, your OpenJDK 8 installation is working correctly.

Troubleshooting

This section covers common issues encountered when installing or using OpenJDK 8 on Ubuntu.

java: command not found

If you receive “java: command not found” after installation, the Java binary may not be in your PATH. Verify the installation completed successfully:

dpkg -l | grep openjdk-8

If packages are listed but java is not found, check the alternatives system:

update-alternatives --display java

If no alternatives are configured, reinstall the package:

sudo apt install --reinstall openjdk-8-jdk

Version Mismatch Between java and javac

Compilation errors like “class file has wrong version” or “unsupported class file major version” indicate that java and javac are pointing to different Java versions. Verify both are using OpenJDK 8:

java -version
javac -version

If the versions differ, configure both alternatives to use the same Java version:

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

Select the same OpenJDK 8 entry in both menus.

Could Not Find or Load Main Class

This error typically occurs due to classpath issues. Ensure you are running the java command from the same directory containing your compiled .class file:

ls -la Hello.class
java Hello

Do not include the .class extension when running the program. If your class is inside a package, you must run it from the root of the package structure with the fully qualified class name.

JAVA_HOME Not Set Correctly

Build tools like Maven or Gradle may fail if JAVA_HOME is not set or points to the wrong location. Verify the current value:

echo $JAVA_HOME

If empty or incorrect, set it temporarily for your current session:

export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))

For persistent configuration across sessions, see our guide on setting the Java environment path in Ubuntu.

Remove OpenJDK 8

If you no longer need OpenJDK 8, remove it along with any automatically installed dependencies:

sudo apt remove openjdk-8-jdk openjdk-8-jre
sudo apt autoremove

The first command removes the main OpenJDK 8 packages, while apt autoremove cleans up any orphaned dependencies that were installed alongside Java but are no longer needed.

After removal, verify that Java is no longer available or has switched to another installed version:

java -version

If no other Java versions are installed, this command returns an error confirming removal. If another version remains, the output shows which Java is now active.

Conclusion

You now have OpenJDK 8 installed on Ubuntu and configured as your Java runtime. With the update-alternatives system, you can switch between multiple Java versions as needed for different projects. For Java development workflows, consider installing Apache Maven as a build tool. If your projects can migrate to a more recent LTS release, explore OpenJDK 17 for broad framework compatibility or OpenJDK 21 for virtual threads and modern language features.

Leave a Comment

Let us know you are human: