NOTE: To install Java in a different location such as /usr/local, you must login as the root user to gain the necessary permissions. If you do not have root access, install Java in your home directory or a subdirectory for which you have write permissions.

1. After downloading the .tar.gz file Change to the directory in which you want to install. Type:

cd directory_path_name

Lets say for example, you want to install the software in the /usr/java/ directory, then type:

cd /usr/java/

2. Move the .tar.gz archive binary to the current directory by typing

mv “source directory path/file to be moved” “destination directory where the files has to be moved”
mv /home/user_home/Downloads/jdk-8u101-linux-x64.tar.gz /user/java

(here user_home indicates the name of the loggedin user e.g /home/john/Downloads, /home/sammy/Downloads etc.)

3. Unpack the tarball and install the jdk

tar zxvf jdk-8u101-linux-x64.tar.gz

Delete the .tar.gz file if you want to save disk space or move it to some other location.

Set JAVA_HOME Path

Open terminal go to your account and open .bashrc. using vim .bashrc

Append or Write

export JAVA_HOME=/user/java
export PATH=$PATH:$JAVA_HOME/bin

.bashrc is a shell script that Bash runs whenever it is started interactively. You can put any command in that file that you could type at the command prompt.

After successfully saving the .bashrc file run the below command to re-run the file so that it can run all the newly added commandssource .bashrc