OS: UBUNTU 18.04
I had installed Oracle Java 8 earlier. But decided to switch to open JDK 11.
it's successfully installed.java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)
Then I set the JAVA_HOMEsudo nano /etc/environment File changes: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/$ JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/"
Then executed: source /etc/environment
echo $JAVA_HOME
/usr/lib/jvm/java-11-openjdk-amd64/
But the moment I exit the terminal and then launch the terminal again:I get this output:
▶ echo $JAVA_HOME /usr/lib/jvm/java-8-oracle
which you can see is not what is expected. Please help me out
33 Answers
To Answer the question
The answers and some comments focus on how to set JAVA_HOME to version 11 but the question is partly about "where is version 8 being set?". To answer that see:
Use this command:
$ sudo grep -rnw --exclude-dir={boot,dev,lib,media,mnt,proc,root,run,sys,/tmp,tmpfs,var} '/' -e '/usr/lib/jvm/java-8-oracle' | grep JAVA_HOME
/etc/profile.d/jdk.sh:4:export JAVA_HOME=/usr/lib/jvm/java-8-oracle
/etc/profile.d/jdk.csh:4:setenv JAVA_HOME /usr/lib/jvm/java-8-oracle
/home/rick/.gradle/daemon/4.6/daemon-14831.out.log:53:20:45:23.553 [DEBUG] (... SNIP ...) /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin (... SNIP ...) Configuring env variables: {PATH=/home/rick/bin:/home/rick/.local/bin:/mnt/e/bin:/mnt/e/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin, (... SNIP ...) This is what my system returns. Yours will likely be different. To make a long story short the culprit is /etc/profile.d/jdk.sh:
$ cat /etc/profile.d/jdk.sh
setenv J2SDKDIR /usr/lib/jvm/java-8-oracle
setenv J2REDIR /usr/lib/jvm/java-8-oracle/jre
setenv PATH ${PATH}:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin
setenv JAVA_HOME /usr/lib/jvm/java-8-oracle
setenv DERBY_HOME /usr/lib/jvm/java-8-oracle/dbYou probably surmise /etc/profile.d/jdk.csh needs changing too. Indeed it contains the exact same contents as /etc/profile.d/jdk.sh
To address the XY problem
There is a better way than going through files line by line and changing an 8 to an 11.
The easiest method is using Java installer. From this Ask Ubuntu Q&A: Setting JDK 7 as default
To get a list of your installed Java platforms, run the following command from the terminal:
sudo update-alternatives --config javaThis will give you a list output similar to this:
There are 2 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/lib/jvm/java-6-oracle/jre/bin/java 1070 auto mode 1 /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java 1051 manual mode
* 2 /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java 1069 manual mode
Press enter to keep the current choice[*], or type selection number: 0 The simplest place to set this is in .bashrc which can be done quickly this way:
echo JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/ >> .bashrc
This will make it the last line read as bash initializes and should ensure that JAVA_HOME is correctly set. If you need to do something more complicated you'll want to read up a bit on how terminal initializes. I'd recommend you start by google-ing bashrc.
Try reading these answers in the link. They are regarding making environment variable changes persistent. I think whats happening is after you close the terminal they are reverting. Here's the link:Make Environment variable changes persist