$JAVA_HOME resets after every terminal login

I just installed openjdk-8-jdk. I already had openjdk-7-jdk installed. When I ran echo $JAVA_HOME, it printed /usr/lib/jvm/java-7-openjdk-amd64.

So I tried changing the $JAVA_HOME variable but it resets to above path every time I re-open the terminal. I have tried setting it in /etc/enviroment /etc/profile /etc/bash.bashrc ~/.bashrc but to no avail.

How can I change it? Also, where the $JAVA_HOME is currently set?


Login to your account and open .bash_profile file

$ vi ~/.bash_profile

Set JAVA_HOME as follows using syntax export JAVA_HOME=<path-to-java-bin-directory>/java
If you want changes effected without logout, enter command below:

$ source ~/.bash_profile

If you do the changes in /etc/environment then changes will be for current session only. /etc/profile is right place to update paths permanently.You need to make changes in profile under /etc

follow below steps. this should solve your issue

sudo vim /etc/profile

add below 2 lines :

export JAVA_HOME="Your path to jdk folder"

export PATH=$JAVA_HOME/bin:$PATH

to apply changes immediately run below commmand:

source /etc/profile

in the next system reboot changes will be picked automatically.