Installing Java 7 on Ubuntu
In addition to flup's answer you might also want to run the following to set JAVA_HOME and PATH:
sudo apt-get install oracle-java7-set-default
More information at: http://www.ubuntuupdates.org/package/webupd8_java/precise/main/base/oracle-java7-set-default
sudo apt-get update
sudo apt-get install openjdk-7-jdk
and if you already have other JDK versions installed
sudo update-alternatives --config java
then select the Java 7 version.
This answer used to describe how to install Oracle Java 7. This no longer works since Oracle end-of-lifed Java 7 and put the binary downloads for versions with security patches behind a paywall. Also, OpenJDK has grown up and is a more viable alternative nowadays.
In Ubuntu 16.04 and higher, Java 7 is no longer available. Usually you're best off installing Java 8 (or 9) instead.
sudo apt-get install openjdk-8-jre
or, f you also want the compiler, get the jdk:
sudo apt-get install openjdk-8-jdk
In Trusty, the easiest way to install Java 7 currently is to install OpenJDK package:
sudo apt-get install openjdk-7-jre
or, for the jdk:
sudo apt-get install openjdk-7-jdk
If you are specifically looking for Java 7 on a version of Ubuntu that no longer supports it, see https://askubuntu.com/questions/761127/how-do-i-install-openjdk-7-on-ubuntu-16-04-or-higher .