“which java” in CentOS prints wrong java path


I am not sure why "<strong><em>which java</em></strong>" and "<strong><em>whereis java</em></strong>" 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 "<strong>echo $JAVA_HOME</strong>" below but the same is not reflected in "<strong><em>which java</em></strong>"

Below is what I get in CentOS 6.4:

<em><strong>which java</strong></em>


<hr />

<em><strong>java -version</strong></em>

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)

<hr />

<em><strong>whereis java</strong></em>

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

<hr />

<strong>echo $JAVA_HOME</strong>

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

<hr />


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.


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.


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) -



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




</blockquote> 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 -



</blockquote> 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


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


  • 密集重建
  • Is there a way to have a bit bucket pointer? (C/C++)
  • Jest encountered an unexpected token: SyntaxError: Unexpected Token {
  • How can I use proxies for web requests in Flurl?
  • Grails project - Servlet call - ClassNotFoundException: javax.servlet.AsyncContext
  • Django model outside of django
  • Cursors with postgres, where is the data stored and how many calls to the DB
  • How do I set default timezone in cakephp?
  • how do you recalibrate touch events for a Qt application?
  • JAAS authentication in JMS using WildFly : javax.jms.JMSSecurityException: HQ119032: User: null does
  • Strange LINQ to SQL .Union() error
  • How to programmatically set custom attributes of custom components?
  • Is it possible to write a gherkin step on multiple lines?
  • generic method in Java
  • Create a JSON document from NSObject with SwiftyJSON
  • file_get_contents Adding numbers from 2 text files
  • Passing data between activities android [duplicate]
  • is Security free in Elastic search Stack Features?
  • DBCP Connection properties
  • ERROR: Could not find method armeabi-v7a() for arguments [arm64-v8a]
  • Pandas better way to get rows that has all columns null except one
  • NetworkX: draw graph in layers
  • How to make Expiring/Signed video embed urls
  • How to plot asymnmetric errors with errorbar
  • Clojure; select all nth element from list of lists with unequal size, for n = 1, 2,
  • Display Django Code from a Django template
  • How to use grep to output unique lines of code from a file?
  • Google Geocoding API limit exceeded on cell network, but not on wifi
  • jQuery how to translate livequery to on?
  • What's the difference between using RDFS/OWL and XML?
  • SELECT on JSONField with Django
  • create circular Auto Horizontal Scroll View?
  • Draw 9 patch dotted /dashed line on Android
  • I need a PHP mySQL Login script that directs user to specific URL based on username and password
  • Julia 1.0 UndefVarError - Scope of Variable
  • How to merge objects within array based on attribute
  • how to get the location(lat/lng) on google maps v3 from the location(x,y)
  • Write to .csv file with PHP (Commas in Data Error)