2

I have a Red Hat Linux machine where I am trying to use Java 8.

So I did this:

export JAVA_HOME=/my/path/to/oracle/jdk/1.8/exec
export PATH=$PATH:$JAVA_HOME

However, if I run java -version I get java version "1.7.0_121".

I looked at this similar question, but I don't seem to have the same problem.

If I run $JAVA_HOME/bin/java -version I get java version "1.8.0_72-b15", so JAVA_HOME does point to Java 8.

which java outputs /usr/bin/java.

So how can JAVA_HOME point to Java 8, while java -version points to Java 7?

Community
  • 1
  • 1
o1ctav
  • 111
  • 2
  • 10

4 Answers4

4

I think you did not configured your PATH correctly, try export PATH=$JAVA_HOME/bin:$PATH:

  • Add $JAVA_HOME/bin to $PATH, not $JAVA_HOME, because the full path to java is $JAVA_HOME/bin/java in the description of question.
  • Put your $JAVA_HOME/bin in the head of $PATH, not the tail, otherwise the old java in original $PATH will always be found by shell first.
shizhz
  • 11,715
  • 3
  • 39
  • 49
0

I don't know if it helps, but I had a similar problem on Windows.

After some research I discovered that a copy of older version of java.exe was also put into Windows' system32 folder.

Maybe something similar is happening in your case ... however I cannot tell you which folder to look for on linux... may be something like /sbin,/bin,/usr/bin/ etc.

The older version of the file java.exe should be in some directory that is on your path variable. So, you can try echo $PATH to get ideas as where to look.

Also reversing the order export PATH=$JAVA_HOME/bin:$PATH (as suggested by others) also could work, as the first version encountered on the path would be the new version (1.8).

PKey
  • 3,715
  • 1
  • 14
  • 39
0

The file /usr/bin/java is a symlink. Just change it to link your Java 8 location as follows:

sudo rm /usr/bin/java
sudo ln -s /path/to/java8/jre/bin/java /usr/bin/java

Notice: you might want to change, as well, every other Java symlinks to have a consistent version of all your Java tools (e.g. javac, javap, etc)

acm
  • 2,086
  • 3
  • 16
  • 34
  • default java version must be managed using alternatives, not manually. – LMC Mar 31 '17 at 13:51
  • Default Java version can be managed manually or using `alternatives`, the way that fits you best. – acm Mar 31 '17 at 17:30
0

Default java binary is managed in Linux using alternatives

someuser@linux:~/tmp> /usr/sbin/alternatives --display java | grep 'priority'
/usr/lib64/jvm/jre-1.7.0-openjdk/bin/java - priority 1705
/usr/lib64/jvm/jre-1.8.0-openjdk/bin/java - priority 1805

Java 8 gets the higher priority. Also, Linux does not use $JAVA_HOME to determine the path to a binary so setting it has no effect in this case.

To set default java version using alternatives refer to this answer: https://askubuntu.com/questions/121654/how-to-set-default-java-version

Community
  • 1
  • 1
LMC
  • 10,453
  • 2
  • 27
  • 52
  • This does not answer OP's question which is how to change from a version of Java to other. – acm Mar 31 '17 at 17:33