Through this series we see how to install Java. In this article, we will look specifically at installing on Linux distributions that use apt-get such as Debian and Ubuntu.

Note When attempting to install, we may get a message E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?. If this happens, run sudo apt-get update and try again.

Java 8

By default, available on Ubuntu 16.04; not available on Ubuntu 14.04 or Ubuntu 12.04.

JRE

Console - user@hostname~$

1
sudo apt-get install openjdk-8-jre

JDK

We do not need to install the JRE separately when installing the JDK, it will be included with the JDK install.

Console - user@hostname~$

1
sudo apt-get install openjdk-8-jdk

Java 7

By default, available on Ubuntu 14.04 and 12.04; not available on Ubuntu 16.04.

JRE

Console - user@hostname~$

1
sudo apt-get install openjdk-7-jre

JDK

Console - user@hostname~$

1
sudo apt-get install openjdk-7-jdk

Java 6

By default, available on Ubuntu 14.04 and 12.04; not available on Ubuntu 16.04.

JRE

Console - user@hostname~$

1
sudo apt-get install openjdk-6-jre

JDK

Console - user@hostname~$

1
sudo apt-get install openjdk-6-jdk

Selecting Java Version

JRE

If there are multiple versions of Java installed, we can select which one we want to use like so:

Console - user@hostname~$

1
sudo update-alternatives --config java

Output

1
2
3
4
5
6
7
8
9
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk/jre/bin/java         1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk/jre/bin/java         1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode

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

Currently java 6 is selected, but can switch to java 7 by typing 2 then hitting Enter

Output

1
update-alternatives: using /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode.

JDK

We need to repeat the process for javac if we have the JDK installed.

Console - user@hostname~$

1
sudo update-alternatives --config javac

Output

1
2
3
4
5
6
7
8
9
There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk-amd64/bin/javac   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/bin/javac   1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/bin/javac   1051      manual mode

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

Currently java 6 is selected, but can switch to java 7 by typing 2 then hitting Enter

Output

1
update-alternatives: using /usr/lib/jvm/java-7-openjdk-amd64/bin/javac to provide /usr/bin/javac (javac) in manual mode.

Setting JAVA_HOME

Set the following in /etc/profile.d/java.sh

/etc/profile.d/java.sh

1
2
3
4
5
6
7
8
JRE_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")
JDK_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
if [ "/usr" != $JDK_HOME ]
then
    export JAVA_HOME=$JDK_HOME
else
    export JAVA_HOME=$JRE_HOME
fi

Parts of this series