Search code examples
linuxjava-home

JAVA_HOME doesn't take effect in Linux


please see my command output below:

steven81@LinuxVMzhexin:~$ echo $JAVA_HOME
/usr/local/jdk-10.0.1
steven81@LinuxVMzhexin:~$ cd /usr/local/jdk-10.0.1/
steven81@LinuxVMzhexin:/usr/local/jdk-10.0.1$ ls -l
total 32
drwxr-xr-x   2 root root 4096 Jun 12 23:52 bin
drwxr-xr-x   4 root root 4096 Jun 12 23:52 conf
drwxr-xr-x   3 root root 4096 Jun 12 23:52 include
drwxr-xr-x   2 root root 4096 Jun 12 23:52 jmods
drwxr-xr-x 101 root root 4096 Jun 12 23:52 legal
drwxr-xr-x  12 root root 4096 Jun 12 23:52 lib
-r--r--r--   1 root root  158 Jun 12 23:52 README.html
-rw-r--r--   1 root root 1656 Jun 12 23:52 release
steven81@LinuxVMzhexin:/usr/local/jdk-10.0.1$ ./bin/java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
steven81@LinuxVMzhexin:/usr/local/jdk-10.0.1$ cd ~
steven81@LinuxVMzhexin:~$ java -version
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11)
OpenJDK 64-Bit Server VM (build 25.171-b11, mixed mode)    

From the output the JAVA_HOME has been set to /usr/local/jdk-10.0.1, but why I use this command:

java -version    

I always get the output like this:

openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11)
OpenJDK 64-Bit Server VM (build 25.171-b11, mixed mode) 

how to fix this issue?


Solution

  • Other than JAVA_HOME you might need JAVA_PATH as well. Here what works on my system:

    Add following to your ~/.profile file:

    PATH="/usr/local/jdk-10.0.1/bin:$PATH"
    export JAVA_PATH="/usr/local/jdk-10.0.1"
    export JAVA_HOME="/usr/local/jdk-10.0.1"
    

    It would be much better if you rename your /usr/local/jdk-10.0.1 to something like /usr/local/jdk10.

    From where the JAVA_PATH came?

    I don't remember well but this has problem with some previously installed openjdk. For most of user's JAVA_PATH is not required but all I know is, it helped me solve my problem.