Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"which java" in CentOS prints wrong java path

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


like image 518
sunskin Avatar asked Oct 19 '13 15:10

sunskin


2 Answers

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.

like image 142
Henry Finucane Avatar answered Nov 10 '22 02:11

Henry Finucane


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.

like image 34
Peter Lawrey Avatar answered Nov 10 '22 02:11

Peter Lawrey