Open /root/.bash_profile through your text editor.

vi /root/.bash_profile

And paste the given below two lines

export JAVA_HOME=/usr/java/jdk1.8.0_121
export PATH=/usr/java/jdk1.8.0_121/bin:$PATH

Now enable the Java variable without system restart (On system restart it bydefault set the java variable)

source /root/.bash_profile

Now check the Java version,JAVA_HOME and PATH variables.It should show you correct information as you have set.

java --version
echo $PATH

Other way:

 cd /etc/alternatives/
 rm java
 ln -s /usr/java/jdk1.8.0_121/bin/java java