editing .bashrc does not change JAVA_HOME

1.8k Views Asked by At

I was wanting to change to JAVA 8 in ubuntu.

I changed the JAVA_HOME in ~/.bashrc and also in /etc/environment and sourced it. This caused JAVA_HOME to change.

PepperBoy@PepperBoy-Inspiron-3542:~/Desktop$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/jvm/jre1.8.0_45/bin:/home/PepperBoy/Desktop/hadoopinstallation/hadoop-1.2.1/bin
PepperBoy@PepperBoy-Inspiron-3542:~/Desktop$ echo $JAVA_HOME
/usr/lib/jvm/jre1.8.0_45

PepperBoy@PepperBoy-Inspiron-3542:~/Desktop$ cat /etc/environment 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
export JAVA_HOME="/usr/lib/jvm/jre1.8.0_45/bin/"

However, my java version still kept showing the earlier version.

PepperBoy@PepperBoy-Inspiron-3542:~/Desktop$ java -version
java version "1.7.0_75"
OpenJDK Runtime Environment (IcedTea 2.5.4) (7u75-2.5.4-1~precise1)
OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)

Also, running the below commmand only gives me options for java 6 and 7, not for java 8
PepperBoy@PepperBoy-Inspiron-3542:~/Desktop$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
* 2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode

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

Finally, running the below gave me the symlink which pointed me to the actual java executable the system was using. I edited this to the java 8 which I downloaded, and now my java -version shows the correct version.

PepperBoy@PepperBoy-Inspiron-3542:~/Desktop$ which java
/usr/bin/java
PepperBoy@PepperBoy-Inspiron-3542:~/Desktop$ ls -l /usr/bin/java
lrwxrwxrwx 1 root root 22 Apr 29  2014 /usr/bin/java -> /etc/alternatives/java
PepperBoy@PepperBoy-Inspiron-3542:~/Desktop$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 46 Oct 17  2014 /etc/alternatives/java -> /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

My question is :

  1. If this is the only edit that brings about the change, then why are we editing ~/.bashrc. Looks like all I have to do is run the which or whereis command in linux, which tells me that /usr/bin/java is the actual executable the system is running, and go and edit this to point to the new java version I have downloaded
1

There are 1 best solutions below

3
On

JAVA_HOME is used by other tools that want to invoke Java. They will (usually) look for Java under JAVA_HOME if it's set. If not they will use whatever java is found on the $PATH. If you want to use JAVA_HOME for finding Java in the shell, you have to add it to PATH; there's nothing magical about the way the shell searches for binaries, that lets it do something special when searching for java.

You can use:

export JAVA_HOME=/usr/lib/jvm/jre1.8.0_45

export PATH=$JAVA_HOME/bin:$PATH

Also, on Ubuntu if there are multiple different versions or types of a given package installable at the same time you control this with the alternatives system:

$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

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