If you prefer to install Oracle JDK, a step by step instruction on installing Oracle JDK 8 is explained in this article :
Install Latest Oracle JDK in Ubuntu
Step 1:
Download the latest JDK(jdk-Xuxx-linux-xXX.tar.gz) from this official link.
Step 2:
Open the terminal (Ctrl + Alt + T) and enter the following command.
sudo mkdir /usr/lib/jvm
Step 3:
Enter the following command to change the directory.
cd /usr/lib/jvm
Step 4:
Extract the jdk-Xuxx-linux-xXX.tar.gz file in that directory using this command.
sudo tar -xvzf ~/Downloads/jdk-8u45-linux-x64.tar.gz
Step 5:
Enter the following command to open the environment variables file.
sudo gedit /etc/environment
Step 6:
In the opened file, add the following bin folders to the existing PATH variable.
/usr/lib/jvm/jdk1.8.0_45/bin
/usr/lib/jvm/jdk1.8.0_45/db/bin
/usr/lib/jvm/jdk1.8.0_45/jre/bin
The PATH variables have to be separated by semicolon.
Notice that the installed JDK version is 1.8 update 45. Depending on your JDK version, the paths can be different.
Add the following environment variables at the end of the file.
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_45"
J2REDIR="/usr/lib/jvm/jdk1.8.0_45/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_45"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_45/db"
The environment file before the modification:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
The environment file after the modification:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_45/bin:/usr/lib/jvm/jdk1.8.0_45/db/bin:/usr/lib/jvm/jdk1.8.0_45/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_45"
J2REDIR="/usr/lib/jvm/jdk1.8.0_45/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_45"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_45/db"