"which java" in CentOS prints wrong java path
Asked Answered
C

3

9

I am not sure why "which java" and "whereis java" paths are not correct. I tried to edit ~/.bash_profile and /etc/environment but did not help. The desired path is what is seen in "echo $JAVA_HOME" below but the same is not reflected in "which java"

Below is what I get in CentOS 6.4:

which java

/usr/bin/java


java -version

java version "1.7.0_45"

JAVA(TM) SE Runtime Environment (build 1.7.0_45-b18)

JAVA HotSpot (TM) 64-bit Server VM (build 24.45-b08, mixed mode)


whereis java

java: /usr/bin/java /etc/java /usr/lib/java /usr/share/java


echo $JAVA_HOME

/usr/java/jdk1.7.0_45/jre => desired shows correct when echo $JAVA_HOME


Countable answered 19/10, 2013 at 15:34 Comment(0)
P
10

Run alternatives --config java to pick the Java version you want to use as default. It will print out a list of installed Javas to choose from.

which java, however, will always print out /usr/bin/java. This doesn't mean it's set wrong! Observe:

$ ls -l `which java`
lrwxrwxrwx 1 root root 22 Oct 19 11:49 /usr/bin/java -> /etc/alternatives/java
$ ls -l /etc/alternatives/java
lrwxrwxrwx 1 root root 35 Oct 19 11:49 /etc/alternatives/java -> /usr/lib/jvm/jre-1.5.0-gcj/bin/java

If you use alternatives to change the path to IcedTea, ls -l /etc/alternatives/java will reflect that.

Parting answered 19/10, 2013 at 15:45 Comment(5)
i tried it already shows the desired one as selected but why it does not reflect in "which java"Countable
Great! Thank you. I never thought of that was symlinked to actual java. I was always in an impression that "which java" is printing wrongCountable
one correction, I had to do ls -l /usr/bin/java instead of ls -l 'which java' but nevertheless thanks for your help. Hav a good one!Countable
Those are backticks, not single quotes, which evaluate to '/usr/bin/java' before the ls command is run. Shell magic :)Parting
Thanks alot, its always a mess configuring and understanding these java pathsGonzalogoo
U
9

Your PATH (and nothing else) determines which directories to look for commands. This is the same in Linux, Solaris, and DOS.

When you do a which {command} it find the first directory you can execute the command in.

When you update your PATH in .bashrc, you have to source it again to change your current settings.

Underproduction answered 19/10, 2013 at 15:39 Comment(4)
i tried "source ~/.bashrc" as root after editing bashrc and it gave me "bash: export: '/usr/java/jdk1.7.0_45/jre=/usr/java/jdk1.7.0_45/jre' : not a valid identifierCountable
That means you have a syntax error in your .bashrc. My bet is you have written something like $JAVA_HOME=/usr/java/jdk1.7.0_45/jre Remove the $ BTW This won't change your PATH which is what matters for whichUnderproduction
+1 thanks peter. that helped but I had to accept Henry answer as he provided an explanation of why 'which java' is pointing to a location which I misunderstoodCountable
thanks Peter, this was driving me crazy, I simply prefixed the 1.6 bin path to the PATH environment variable, relogged and now "which" works as expected. I make a note of this here as this is now required for building the android sources. Appreciated.Foramen
K
0

Sometimes alternatives does not work in a single command by selecting the desired version of java. I am not sure of the precise reason for this though..

I fell victim to such a scenario. The auto-pilot failed, we must fly manual now..

In any of below two files in your unix installation add following variables and a call to a shell script (I have provided below) -

~/.bashrc

export JAVA_HOME=/opt/jdk1.8.0_141
export PATH=$JAVA_HOME/bin:$PATH
sudo bash /appl/common/toFixJava.sh

OR

/etc/profile

export JAVA_HOME=/opt/jdk1.8.0_141
export PATH=$JAVA_HOME/bin:$PATH
sudo bash /appl/common/toFixJava.sh

The script below referenced above sets the rest of java modules to utilize the java you want to use -

toFixJava.sh

altrs="java javac jre jarsigner javadoc javafxpackager javah javap java-rmi.cgi javaws jcmd jconsole jcontrol jdb jhat jinfo jmap jmc jmc.ini jps jrunscript jsadebugd jstack jstat jstatd jvisualvm keytool appletviewer apt ControlPanel extcheck idlj native2ascii orbd pack200 policytool rmic rmid rmiregistry schemagen serialver servertool tnameserv unpack200 wsgen wsimport xjc"

for each in $altrs do

alternatives --install /usr/bin/$each $each /opt/jdk1.8.0_141/bin/$each <desired installation index number, e.g. 2>
alternatives --set $each /opt/jdk1.8.0_141/bin/$each

done

Please do let know if this doesn't work for you. I will help you solve alternatively!

Kalb answered 22/7, 2017 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.