Install Oracle JDK 1.8 and Set environment variables in Ubuntu

Step-by-step guide on installing JDK8(Version 1.8)

Some of the environments or projects you work with may specifically need the Java8 (JDK-1.8) for the references. In such instances, the ideal solution is to download the jdk.tar.gz file from the oracle.com and install it from the terminal without depending to apt-get or any other.

The following is the step-by-step guide of how to install and set the environment-variable paths successfully.

Download the JavaSE Development Kit 8 from the given link below.
URL : https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html (Linux x64 : jdk-8u211-linux-x64.tar.gz)

Download the above tar.gz file to “/home/hirosht/Downloads”.
(You might have to login to the Oracle Account to download)

Ps: If any other version of OracleJDK / OpenJDKis installed remove all firstly.

  1. Open the Terminal (Ctrl + Alt + T)
  2. Create the jvm folder in usr/lib
    $ sudo mkdir /usr/lib/jvm
  3. Change the working directory
    $ cd /usr/lib/jvm
  4. Extract the downloaded jdk-xuxxx-linux-x64.tar.gz file in the ~/Downloads folder using this command
    $ sudo tar -xvzf ~/Downloads/jdk-8u211-linux-x64.tar.gz
  5. Open the Environment Variable file using Nano-editor or Vi-editor
    $ sudo nano /etc/environment or $ sudo vi /etc/environment
  6. Add the following ‘bin-folder-path’ to the existing PATH variable in the opened file (ensure to separate the Path variable by a semicolon [:])
    /usr/lib/jvm/jdk-11/bin
  7. Add the following JAVA_HOME environment variables at the end of the file. JAVA_HOME="/usr/lib/jvm/jdk-11"

The modified environment file must looks like following.

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_211/bin"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_211"

8. Save the changes based on the editor opened

— 8.1 If Nano; 
— — - Ctrl + O and then Ctrl + X
— 8.2 If Vi;
— — - Press Escape.
— — - Then Shift + Semicolon (:).
— — — Afterwards type (:wq!) and Press Enter

9. Set the Java Location

$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_211/bin/java" 0$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_211/bin/javac" 0$ sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_211/bin/java$ sudo update-alternatives --set javac /usr/lib/jvm/jdk1.8.0_211/bin/javac

10. Verify the setup by executing the following commands.

$ update-alternatives --list java
$ update-alternatives --list javac

11. Open the ~/.bashrc with vi-editor to append the following environment variables to the bottom.

$ sudo vi ~/.bashrc

and Press ‘i’ to move into insert in vi-editor. Add the following to the bottom of the doc.

JAVA_HOME=”/usr/lib/jvm/jdk1.8.0_211"
MAVEN_HOME=”/usr/share/maven”
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin:$MAVEN_HOME/bin
export JAVA_HOME
export MAVEN_HOME
export PATH

(Ps: I have set the MAVEN_HOME path as well additionally. If its not required please remove those lines before pasting in ‘.bashrc’).

Save the amendment in the doc in vi-editor by following the steps below.

Press Escape. 
Then Shift + Semicolon (:).
Afterwards type (:wq!)
and Press Enter

12. Verify the Java Version (Restart, if required)

$ java -version

Should display as

13. Verify the JAVA_HOME path.

$ echo $JAVA_HOME

the path should display as follows,

Successfully installed and configured! Feel free to comment below if there is a better approach or if the flow can be improved.

Writer | Vlogger | Techie | Technical Tester (Snr-QAE)

Writer | Vlogger | Techie | Technical Tester (Snr-QAE)