1

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
tubby
  • 2,074
  • 3
  • 33
  • 55

1 Answers1

1

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:
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • thanks. But editing the JAVA_HOME and PATH did not bring about any change in my java -version, It was only after changing the symlink for /usr/bin/java that java -version showed the new version. So my question was, why are we then setting JAVA_HOME if adding it/not adding it does not make any difference? – tubby Jun 14 '15 at 18:38
  • As I said, it does make a difference to _other programs_ that want to find Java, and obey `JAVA_HOME`. Your shell doesn't treat `JAVA_HOME` as special in any way, it only looks at `PATH` to find all binaries. But other tools that want to find Java, such as maven, etc. will obey `JAVA_HOME` if it's set. As for changing your PATH and having it not take effect, you must have either not done it right, or perhaps forgotten to flush the shell's cache of binaries with `hash -r` (although you shouldn't need to do that as setting `PATH` should clear it). – MadScientist Jun 14 '15 at 18:47
  • Just to be clear, the reference to `update-alternatives` shows the proper way to _change the symlink_, rather than doing it by hand. Any time you see a symlink in `/bin` or `/usr/bin` (or another system directory) that resolves through `/etc/alternatives`, that package is one of an alternative and if you want to change the default alternative it's best to use the `update-alternatives` utility, _NOT_ modify the symlink yourself by hand. – MadScientist Jun 14 '15 at 19:03