I am on Ubuntu and I have set the following in my ~/.bashrc
file:
export JAVA_HOME=/opt/jdk1.8.0_91
export PATH=$JAVA_HOME/bin:$PATH
and then:
echo $JAVA_HOME
>/opt/jdk1.8.0_91
java -version
>java version "1.8.0_91"
>Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
>Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
at the first glance, the command sudo update-alternatives --config java
was not showing my manually installed Java, so I installed it to the command with sudo update-alternatives --install /usr/bin/java java /opt/jdk1.8.0_91 1
.
Now, the command sudo update-alternatives --config java
drops down the list of all Java versions being installed like that:
0 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 auto mode
1 /opt/jdk1.7.0_51/bin/java 1 manual mode
* 2 /opt/jdk1.8.0_91 1 manual mode
3 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
4 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode
But switching between these using the prompt of the sudo update-alternatives --config java
does not affect $JAVA_HOME
and then $java -version
.
My question is, what does sudo update-alternatives --config java
exactly do after switching to another alternative with respect to the settings in the $JAVA_HOME$
variable?
It only changes a symlink located (on most distro I guess) at /etc/alternatives/java
.
Absolutely NO change in the environment variable you set $JAVA_HOME
is made.
First look at from where the command is found, you can do :
$which java
/usr/bin/java
The which command shows /usr/bin/java
in my Debian distro. This file is a symlink which points to /etc/alternatives/java
.
$ls -l /usr/bin | grep java
java -> /etc/alternatives/java
Then you follow the symlink :
$ls -l /etc/alternatives/java
/etc/alternatives/java -> /path/to/my/java/installation/1.x/bin/java
This shows that /etc/alternatives/java
is another symlink.
When you do an update-alternatives
on java, you just change this symlink target to another one.
Then, why doesn't the executed version change when you do the update-alternatives
command ?
I guess it's because of the order the executables are found in $PATH
.
Since you added a directory to the PATH environment variable, there are now two possible java executables : one in /usr/bin
and the other in /opt/jdk1.8.0_9
, but only the first one found will be taken into account when you'll type java
commands.
And because you set
PATH=$JAVA_HOME/bin:$PATH
The first one will be found in $JAVA_HOME/bin
aka /opt/jdk1.8.0_91
.
Because you made /opt/jdk1.8.0_9
appear before /usr/bin
which is defined by default in the the PATH variable.
You can check it by typing in a terminal
$echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/path/to/my/java/installation/1.x/bin
You can see that my java/bin dir is located after the others defined in the PATH.
To correct this, you just have to concatenate $JAVA_HOME/bin
after $PATH
, like this :
PATH=$PATH:$JAVA_HOME/bin
This way you will be able to choose the default java executable from alternatives and the java exe found in $JAVA_HOME/bin
will be discarded.
But to be consistent, in most cases you should choose the same java exe as in $JAVA_HOME/bin
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With