In 2020 here is how we roll!
NB: This will also work for Command Line on Linux Systems.
Kudos to this dude here
Step 1:
Download the latest JDK(jdk-8u231-linux-x64.tar.gz) from this official site.
using this
wget -c --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u231-linux-x64.tar.gz
because we are installing remotely if not just download from here
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-8u231-linux-x64.tar.gz file in that directory using this command.
sudo tar -xvzf ~/Downloads/jdk-8u231-linux-x64.tar.gz
According to this command, the JDK filename is jdk-8u231-linux-x64.tar.gz and which is located in the ~/Downloads folder. If your downloaded file is in any other location, change the command according to your path.
Step 5:
Enter the following command to open the environment variables file.
sudo nano /etc/environment
In the opened file, add the following bin folders to the existing PATH variable. (in my case the file was empty so i just add the content and saved).
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_231/bin:/usr/lib/jvm/jdk1.8.0_231/db/bin:/usr/lib/jvm/jdk1.8.0_231/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_231"
J2REDIR="/usr/lib/jvm/jdk1.8.0_231/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_231"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_231/db"
This is how your file will look after modifying...
Save the changes and close the gedit.
Step 7:
Enter the following commands to inform the system about the Java's location. Depending on your JDK version, the paths can be different.
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_231/bin/java" 0
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_231/bin/javac" 0
sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_231/bin/java
sudo update-alternatives --set javac /usr/lib/jvm/jdk1.8.0_231/bin/javac
Step 8:
To verify the setup enter the following commands and make sure that they print the location of java and javac as you have provided in the previous step.
update-alternatives --list java
update-alternatives --list javac
Step 9:
Restart the computer (or just log-out and login) and open the terminal again. (I dint have to do this because step 10 already came through)
Step 10:
Enter the following command.
java -version
Step 11:
Enjoy!!!