5

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?

  • Is there anything in ~/.profile that shouldn't be there? Just a stab in the dark... – Ken Sharp Jan 26 '16 at 6:03
4

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
2

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
  • Does sudo update-alternatives --config java sets the JAVA_HOME variable too? – suheb Jan 26 '16 at 9:25
  • 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. – Anders Olsson Jan 26 '16 at 9:36
2

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.

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.