"which java" in CentOS prints wrong java path

24,624

Solution 1

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.

Solution 2

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.

Share:
24,624
sunskin
Author by

sunskin

Android is in the Air!

Updated on July 09, 2022

Comments

  • sunskin
    sunskin almost 2 years

    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


  • sunskin
    sunskin over 10 years
    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 identifier
  • sunskin
    sunskin over 10 years
    i tried it already shows the desired one as selected but why it does not reflect in "which java"
  • Vishy
    Vishy over 10 years
    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 which
  • sunskin
    sunskin over 10 years
    Great! Thank you. I never thought of that was symlinked to actual java. I was always in an impression that "which java" is printing wrong
  • sunskin
    sunskin over 10 years
    +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 misunderstood
  • sunskin
    sunskin over 10 years
    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!
  • Henry Finucane
    Henry Finucane over 10 years
    Those are backticks, not single quotes, which evaluate to '/usr/bin/java' before the ls command is run. Shell magic :)
  • twobob
    twobob over 10 years
    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.
  • nuicca
    nuicca over 3 years
    Thanks alot, its always a mess configuring and understanding these java paths