$JAVA_HOME resets after every terminal login

16,497

Solution 1

I figured it out. It was ridiculous mistake by me.

As I set JAVA_HOME in so many files, in all the mess I set it to /usr/lib/jvm/java-7-openjdk-amd64 in ~/.bashrc. So every time I opened the terminal it changed to this no matter what was in all the other files, as ~/.bashrc runs every time teminal is opened.

Solution 2

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

Solution 3

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.

Solution 4

You should not set this yourself, if it can be helped. To change which Java version to use, see this guide.

Snippet:

If your system has more than one version of Java, configure which one your system uses by entering the following command in a terminal window

sudo update-alternatives --config java

This will present you with a selection that looks similar to the following (the details may differ for you):

There are 2 choices for the alternative java (providing /usr/bin/java).  
Selection Path Priority Status 
———————————————————— 
* 0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 auto mode 
1 /usr/lib/jvm/jre1.7.0/jre/bin/java 3 manual mode  

Press enter to keep the current choice[*], or type selection number: 1
Share:
16,497

Related videos on Youtube

suheb
Author by

suheb

Updated on September 18, 2022

Comments

  • suheb
    suheb over 1 year

    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?

    • Ken Sharp
      Ken Sharp over 8 years
      Is there anything in ~/.profile that shouldn't be there? Just a stab in the dark...
  • suheb
    suheb over 8 years
    Does sudo update-alternatives --config java sets the JAVA_HOME variable too?
  • Anders Olsson
    Anders Olsson over 8 years
    Sorry, if it was specifically only about $JAVA_HOME your question was about, my answer was not well researched. On my system (14.04.03) $JAVA_HOME is not set at all.