In the process of installing OpenJDK 11 (via a download), and attempting to set the environment variable and defaults to invoke this version instead of the previously installed Java 10, I have messed up the ability of Ubuntu to find Java. The current error message follows:
phil@UBI-VirtualBox:~/JavaApps$ java -version bash: /usr/bin/java: No such file or directory
The following is the chronology of actions that led to this problem.
I installed OpenJDK 11 in /usr/lib/jvm/jdk-11.0.1 file folder. The /usr/lib/jvm folder also has other Java versions (e.g., /java-ll-openjdk-amd64, which actually runs java 10).
I then created a JAVA_HOME variable and set it with the preferred java version. The environment variable is now listed in both /etc/environment
JAVA_HOME="/usr/lib/jvm/jdk-11.0.1"
and as the last line in ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/jdk-11.0.1
At that point, when attempting to check via "java -version" I found that JAVA_HOME works fine, but that my default still pointed to the previously installed Java 10.
phil@UBI-VirtualBox:~$ java -version
openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4, mixed mode)
phil@UBI-VirtualBox:~$ $JAVA_HOME/bin/java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)
It was at this point I figured out that update-alternatives was playing a role.
Unfortunately, I seem to have screwed things up by putting in an erroneous command:
phil@UBI-VirtualBox:~$ sudo update-alternatives --install /usr/lib/jvm java /usr/lib/jvm/jdk-11.0.1/bin/java 3
update-alternatives: renaming java link from /usr/bin/java to /usr/lib/jvm
update-alternatives: warning: forcing reinstallation of alternative /usr/lib/jvm/java-11-openjdk-amd64/bin/java because link group java is broken
update-alternatives: warning: not replacing /usr/lib/jvm with a link
phil@UBI-VirtualBox:~$ sudo update-alternatives --install /usr/lib/java java /usr/lib/jvm/jdk-11.0.1/bin/java 3
update-alternatives: warning: forcing reinstallation of alternative /usr/lib/jvm/java-11-openjdk-amd64/bin/java because link group java is broken
phil@UBI-VirtualBox:~$ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/lib/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1101 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
3 /usr/lib/jvm/jdk-11.0.1/bin/java 3 manual mode
Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/lib/jvm/jdk-11.0.1/bin/java to provide /usr/lib/java (java) in manual mode
Now, when running "java -version" I get the following error message:
phil@UBI-VirtualBox:~/JavaApps$ java -version
bash: /usr/bin/java: No such file or directory
Inspecting /usr/bin for a "java" file, I find that none exist. There is a "javac" link that points to the /ext/alternatives folder (as well as other links such as "jar").
The /ext/alternatives folder has both a "javac" and "java" links in it. Both "javac" and "java" point to the desired java version. (I had also added the alternatives to javac.)
In addition, the folder that holds the jvms (/usr/lib/jvm) also holds yet another "java" link file. This one points to "/etc/alternatives/java". And there is a "default-java" file-folder link that points to the undesired Java version.
I tried copying the link to "/etc/alternatives/java" from /usr/lib/jvm to /usr/bin, but the resulting icon did not have an arrow and did not work, so I deleted it. Command used to copy:
sudo cp /usr/lib/jvm/java /usr/bin/java
I thought I better stop thrashing around before making things even worse and ask how to fix this.
BTW, JAVA_HOME still works fine.
phil@UBI-VirtualBox:~$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-11.0.1/bin/java 3
, then runsudo update-alternatives --config java
and fix it there!