Explain Codes LogoExplain Codes Logo

Installing Java 7 on Ubuntu

java
java-7
update-alternatives
java-version-management
Nikita BarsukovbyNikita Barsukov·Sep 2, 2024
TLDR

To install OpenJDK 7 simply on Ubuntu, use these terminal commands:

sudo apt-get update sudo apt-get install openjdk-7-jdk

Confirm installation with java -version. You should see Java 7 active on your system. For Ubuntu 16.04 or higher, use a PPA (Personal Package Archive) as Java 7 isn't available in official repositories.

The vintage charm of Java 7

Despite its age, much like fine wine, Java 7 might be necessary for certain software or development needs. Just like grandma swearing by her old pressure cooker instead of that shiny new Instant Pot, old is gold.

Know what you're doing

Ensure you need Java 7. Newer versions, mainly Java 8 and onwards, often offer enhanced features and security. However if Java 7 is non-negotiable for your scenario, proceed with these instructions.

Master the art of juggling Java versions

When managing multiple Java versions on the same system, update-alternatives is your best friend. This utility helps manage multiple versions, allowing you to prioritize the default.

Playing with alternatives

Add Java binaries to the alternatives system with:

sudo update-alternatives --install /usr/bin/java java /path/to/java 1 sudo update-alternatives --install /usr/bin/javac javac /path/to/javac 1

It's like playing Minecraft, but with Java versions. Block, meet World!

Swapping defaults

To switch the default Java version, use:

sudo update-alternatives --config java

Play Eenie-Meenie-Miney-Moe with the numbers corresponding to each version.

Testing the setup

After, verify whether your IDE like Eclipse, IntelliJ, or NetBeans recognizes Java 7 under its Java version settings. Finally, confirm the Java version in terminal, to ensure no sneaky gremlins caused a switch back:

java -version

Ensure the output falls in line with Java version "1.7.0".

Setting JAVA_HOME

Setting JAVA_HOME is crucial, much like keeping your wand close if you're Harry Potter. Add this to your .bashrc or .profile:

export JAVA_HOME=$(dirname $(dirname $(update-alternatives --list javac))) export PATH=$JAVA_HOME/bin:$PATH

This sets JAVA_HOME to the location of the current javac version.

Systemic Harmony

Beware, using a PPA for an unsupported package could lead to system instability. Regular system backups are advised before such adventurous endeavours. Think Indiana Jones in a maze guarding relics.

Oracle vs OpenJDK

Oracle's Java 7 is another option, like choosing between Coke and Pepsi, if you're into that sort of thing. Use the oracle-java7-installer package to automate downloading and installing from official sources.