How-to Install Java 7 on Ubuntu 16.04

Overview

Java is a programming language first released by Sun Microsystem in 1995. It’s still being developed now even after Sun Microsystem bought by Oracle.

Java 7 is actually the previous version of Java that already reach end of life. However, some application stil run only on Java 7 or compatible only to Java 7 and not Java 8.

Objective

In this tutorial we’ll install Java 7 on Ubuntu 16.04. We’ll learn how-to install both Java Runtime Environment (JRE) and Java Development Kit (JDK).

We’ll cover Java installation from OpenJDK and from Oracle.

Install Java 7 on Ubuntu From OpenJDK

Ubuntu 16.04 Xenial Xerus shipped with Java 8 from OpenJDK. Therefore, we’ll install Java 7 from openjdk-r PPA repository. Let’s get started.

Add openjdk-r PPA repository.


$ sudo add-apt-repository ppa:openjdk-r/ppa
 
 More info: https://launchpad.net/~openjdk-r/+archive/ubuntu/ppa
Press [ENTER] to continue or ctrl-c to cancel adding it

Press ENTER.


gpg: keyring '/tmp/tmpccfu5qmx/secring.gpg' created
gpg: keyring '/tmp/tmpccfu5qmx/pubring.gpg' created
gpg: requesting key 86F44E2A from hkp server keyserver.ubuntu.com
gpg: /tmp/tmpccfu5qmx/trustdb.gpg: trustdb created
gpg: key 86F44E2A: public key "Launchpad OpenJDK builds (all archs)" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

openjdk-r repository successfully added.

Re-read apt-get package metadata by running command below.


sudo apt-get update

After re-reading metadata, let’s search what packages available for openjdk-7.


$ sudo apt-cache search openjdk-7
openjdk-7-jdk - OpenJDK Development Kit (JDK)
openjdk-7-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-7-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-7-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-7-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-7-jre-lib - OpenJDK Java runtime (architecture independent libraries)
openjdk-7-source - OpenJDK Development Kit (JDK) source files
openjdk-7-doc - OpenJDK Development Kit (JDK) documentation
openjdk-7-jre-zero - Alternative JVM for OpenJDK, using Zero/Shark

As you can see from output above there are three alternative to install Java 7 from OpenJDK.

  • Java Runtime Environment (JRE) – openjdk-7-jre. This is normal JRE with keyboard and monitor support.
  • Java Runtime Environment Headless – openjdk-7-jre-headless. This is JRE without keyboard and monitor support, ideal for installation on server.
  • Java Development Kit 7 (JDK) – openjdk-7-jdk. This package will install JDK that include javac compiler.

Install Java Runtime Environment 7 (JRE 7)

To install OpenJDK JRE 7 you can use command below.


$ sudo apt-get install openjdk-7-jre

[OpenJDK Java Runtime Environment 7 on Ubuntu 16.04][1]

Install Java Runtime Environment 7 Headless

To install OpenJDK JRE 7 headless you can use command below.


$ sudo apt-get install openjdk-7-jre-headless

[JRE headless on ubuntu 16.04][2]

Install Java Development Kit 7 (JDK 7)

To install OpenJDK JDK 7 you can use command below.


$ sudo apt-get install openjdk-7-jdk

[JDK 7 on Ubuntu 16.04][3]

Install Oracle Java 7

Now let’s learn how-to install Oracle Java 7.

Previously we can easily download Oracle Java 7. Webupd8 even provide PPA repository to install Oracle Java 7.

Since Oracle Java 7 download moved to [Java archive download][4] where we have to login we can’t install Java 7 from Webupd8 PPA repository anymore.

The latest publicly released Oracle Java 7 version is Java 7 Update 80.

Please note this is not the latest version of Java 7, not recommended to be used in production.

Install Oracle Java Runtime Environment 7 (JRE 7)

We assume that you already downloaded jre-7u80-linux-x64.tar.gz file from [Java archive download][4] page. If you are using 32 bit machine you can download file jre-7u80-linux-i586.tar.gz.

Extract the downloaded file.


$ tar xzf jre-7u80-linux-x64.tar.gz

the command above will extract contents of jre compressed archive under jre1.7.0_80 directory.

Move this directory to /opt. We will install Oracle JRE 7 to /opt since this directory is reserved for application not shipped with operating system itself.


$ sudo mv jre1.7.0_80/ /opt/

Change the ownership of the directory and all of its contents to user root and group root.


$ sudo chown -R root:root /opt/jre1.7.0_80/

Add the jre 7 as default java


$ sudo update-alternatives --install /usr/bin/java java /opt/jre1.7.0_80/bin/java 100

Install Oracle Java Runtime Environment 7 Server

Oracle Java Runtime Environment 7 Server or usually known as headless JRE is only available for 64 bit machine. You can download file server-jre-7u80-linux-x64.tar.gz.

The installation process is similar to Oracle JRE 7 installation above

Install Oracle Java Development Kit 7 (JDK 7)

Download file jdk-7u80-linux-x64.tar.gz.

Extract the file using command below


$ tar xzf jdk-7u80-linux-x64.tar.gz 

Move the extracted directory to /opt


$ sudo mv jdk1.7.0_80/ /opt/

Change ownership of the directory to user and group root.


$ sudo chown -R root:root /opt/jdk1.7.0_80/

Set the JDK java as default java


$ sudo update-alternatives --install /usr/bin/java java /opt/jdk1.7.0_80/bin/java 200

Set the JDK javac as default javac


$ sudo update-alternatives --install /usr/bin/javac javac /opt/jdk1.7.0_80/bin/javac 200

To see list of available java installed you can use command below:


$ update-alternatives --list java
/opt/jdk1.7.0_80/bin/java
/opt/jre1.7.0_80/bin/java

To manually change the default java you can use command below.


$ update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                       Priority   Status
------------------------------------------------------------
* 0            /opt/jdk1.7.0_80/bin/java   200       auto mode
  1            /opt/jdk1.7.0_80/bin/java   200       manual mode
  2            /opt/jre1.7.0_80/bin/java   100       manual mode

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

You can manually choose which java installation that you want to use by entering selection number.

Compiling Java 7 Application

Now let’s try creating our Hello World Java application. Create new file HelloHowtoDojo.java with contents below:


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

```

Compile the code using command below

```

$ javac HelloHowtoDojo.java

```

This will create `HelloHowtoDojo.class` file.

To run the application you can use command below

```

$ java HelloHowtoDojo
Hello HowtoDojo!

```

## Summary

In this tutorial we learned how-to install Java 7 on Ubuntu 16.04. Since Java 7 is not shipped in Ubuntu 16.04 Xenial Xerus we install the package from openjdk-r PPA repository.

We also learned installing Oracle Java 7 by downloading the archive and install it manually.

At the end of this tutorial we learned how to create simple Hello HowtoDojo application, compile the code and run our first Java application.

 [1]: https://www.howtodojo.com/wp-content/uploads/2017/07/openjdk-7-jre-ubuntu-16.04.png
 [2]: https://www.howtodojo.com/wp-content/uploads/2017/07/openjdk-7-jre-headless-ubuntu-16.04.png
 [3]: https://www.howtodojo.com/wp-content/uploads/2017/07/openjdk-7-jdk-ubuntu-16.04.png
 [4]: http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html