Search code examples
javalinuxjava-home

Different version of java displayed in $JAVA_HOME and java -version in linux


I am using Cent-OS.

When I execute "java -version", it displays: java version "1.7.0_45"

When i execute "echo $JAVA_HOME", it displays /usr/java/jdk1.8.0_51

I set JAVA_HOME in ~/.bashrc and sourced it. I also tried setting in /etc/profile and sourced it as well. In profiles, I am setting java home as:

JAVA_HOME=/usr/java/jdk1.8.0_51

export PATH=$PATH:$JAVA_HOME/bin

Please tell me how to set java version to 1.8 only.


Solution

  • I believe 1.7.0_45 is set in your path already, and in order to use 1.8.0_51, export the path as follows,

    export PATH=$JAVA_HOME/bin:$PATH 
    

    This will add new JAVA path in front of the existing java path. The issue in your export is the new java path is added after the existing path. Hope this helps.