Java installation
Ubuntu
Java 7
Linux
Software setup

Installing Java 7 on Ubuntu

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Installing Java 7 on Ubuntu is now a legacy maintenance task, usually needed for old enterprise builds or third-party tools. Since Java 7 is end-of-life, packages are no longer first-class in modern Ubuntu repositories. The practical approach is to install it in an isolated environment, configure alternatives, and keep risk bounded.

Understand the Risk Before Installing

Java 7 no longer receives public security updates in normal channels. Running it on internet-facing services is high risk. Use it only for constrained use cases such as local builds, migration testing, or offline legacy workloads.

Recommended safeguards:

  • isolate with container or VM,
  • restrict network access,
  • avoid using Java 7 as global system default,
  • document why legacy runtime is required.

If your application can run on Java 8 or newer, migrate instead of extending Java 7 usage.

Option 1: Install Java 7 from a Vendor Archive Tarball

On modern Ubuntu versions, tarball installation is often more reliable than old apt repositories.

Create runtime directory and extract archive:

bash
sudo mkdir -p /opt/java
sudo tar -xzf jdk-7u80-linux-x64.tar.gz -C /opt/java
ls /opt/java

Register binaries with update-alternatives:

bash
1sudo update-alternatives --install /usr/bin/java java /opt/java/jdk1.7.0_80/bin/java 1070
2sudo update-alternatives --install /usr/bin/javac javac /opt/java/jdk1.7.0_80/bin/javac 1070
3sudo update-alternatives --config java
4sudo update-alternatives --config javac

Verify:

bash
java -version
javac -version

This keeps installation explicit and easy to remove later.

Option 2: Use a Docker Image for Safer Isolation

Containerizing legacy Java is often the cleanest operational choice.

Example Dockerfile:

dockerfile
1FROM ubuntu:20.04
2
3RUN apt-get update && apt-get install -y wget ca-certificates tar && rm -rf /var/lib/apt/lists/*
4WORKDIR /opt/java
5RUN wget https://example.local/jdk-7u80-linux-x64.tar.gz \
6    && tar -xzf jdk-7u80-linux-x64.tar.gz \
7    && rm jdk-7u80-linux-x64.tar.gz
8
9ENV JAVA_HOME=/opt/java/jdk1.7.0_80
10ENV PATH="$JAVA_HOME/bin:$PATH"
11
12CMD ["java", "-version"]

Build and run:

bash
docker build -t java7-runtime .
docker run --rm java7-runtime

This prevents accidental host-wide runtime downgrades.

Configure JAVA_HOME for Shell Sessions

After installation, define environment variables for tooling that expects JAVA_HOME.

bash
echo 'export JAVA_HOME=/opt/java/jdk1.7.0_80' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

Check values:

bash
echo "$JAVA_HOME"
which java
java -version

For CI pipelines, set these values per job instead of relying on user shell profiles.

Manage Multiple Java Versions Cleanly

Legacy projects often require Java 7 while modern projects need Java 11 or 17. Use update-alternatives to switch deliberately.

List choices:

bash
sudo update-alternatives --config java

Switch only for the current task, then switch back. Avoid leaving Java 7 as default for unrelated builds.

If you prefer per-project switching without global changes, tools such as SDKMAN can manage local shell-level runtime selection.

Validate with a Small Compile and Run Test

Run an explicit compile test to confirm javac and java point to the same version.

HelloLegacy.java:

java
1public class HelloLegacy {
2    public static void main(String[] args) {
3        System.out.println("Hello from Java 7");
4    }
5}

Compile and run:

bash
javac HelloLegacy.java
java HelloLegacy

If this fails with class version errors, you likely compiled with one JDK and executed with another.

Troubleshooting Notes

If java -version still shows a newer JDK, re-check alternatives registration and shell path order.

bash
which java
readlink -f $(which java)

If javac is missing, you installed a JRE instead of a JDK. Install a full JDK package or tarball.

If legacy apps fail with TLS errors, old Java trust stores and cipher defaults are often the cause. In that case, isolate network traffic and plan application migration.

Common Pitfalls

A common mistake is following old tutorials that depend on obsolete PPAs. Many no longer work on current Ubuntu releases.

Another issue is setting Java 7 as system-wide default permanently, which breaks modern build tools.

Teams also skip version verification and assume command paths are aligned. Always verify both java and javac outputs.

Finally, running Java 7 workloads on open networks without controls creates avoidable security exposure.

Summary

  • Java 7 installation on Ubuntu is a legacy operation and should be isolated.
  • Tarball installation plus update-alternatives is the most predictable path.
  • Containers are often safer than host-wide installation for legacy runtimes.
  • Set JAVA_HOME explicitly and verify java and javac versions together.
  • Keep Java 7 usage temporary and migration-focused whenever possible.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.