Install Java 8 on Ubuntu 16

Problem

Java / JVM (Java's virtual machine) / JDK are popular products and needed by many programs. This post will cover the basic installing steps and managing different versions of Java using apt-get.

Update November 2019: The correct way to install Oracle JDK on Ubuntu

Solution Install default Java

Installing default java package from Ubuntu is best option for most people and situations. It's proven and tested solution satisfying most programs. Depending on your needs you can install only JRE and/or JDK. JDK is required for programmers. This installs open JDK.

sudo apt-get update
sudo apt-get install default-jre
sudo apt-get install default-jdk

Solution Install oracle Java

If you need to have oracle java you can use repository:

sudo add-apt-repository ppa:webupd8team/java

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

If you want to test java 9 you can do it by :

sudo apt-get install oracle-java9-installer

Solution Choose Java version

If you want to change java version you need to type:

sudo update-alternatives --config java

and then to choose your version:

There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      auto mode
  1            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  2            /usr/lib/jvm/java-8-oracle/jre/bin/java          3         manual mode
  3            /usr/lib/jvm/java-9-oracle/bin/java              4         manual mode

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

Solution Setting JAVA_HOME

Last thing after fresh install of Java is to set environmental variable JAVA_HOME. This helps programs to find the installation folder. You can set it by:

  • Check your default installation path and copy it
sudo update-alternatives --config java
  • Open environmental settings file by:
sudo nano /etc/environment
  • Add this line at the end(check you path) and save it:
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
  • Reload the file by:
source /etc/environment
  • Test JAVA_HOME by:
echo $JAVA_HOME

P.S. If you need to set JAVA_HOME please see: Java 8 set JAVA_HOME on Windows 10