0

I already read many similar posts online, but can't seem to arrive at a solution. I want to be able to switch between java versions. Thanks in advance! OS: Ubuntu 16.04.6 LTS x86_64

I already manually installed and set update-alternatives java and javac

sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                        Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-12-oracle/bin/java         1091      auto mode
* 1            /usr/lib/jvm/java-12-oracle/bin/java         1091      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/bin/java   0         manual mode


    sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-12-oracle/bin/javac         1091      auto mode
* 1            /usr/lib/jvm/java-12-oracle/bin/javac         1091      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/bin/javac   0         manual mode

echo $JAVA_HOME
/usr/lib/jvm/java-8-openjdk-amd64

PATH shows /usr/lib/jvm/java-8-openjdk-amd64/bin and /usr/lib/jvm/java-12-oracle/bin

echo $PATH
/home/myname/Repositories/build/depot_tools:/home/myname/bin:/home/myname/.local/bin:/usr/lib/jvm/java-8-openjdk-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/cuda/bin:/snap/bin:/usr/lib/jvm/java-12-oracle/bin:/usr/lib/jvm/java-12-oracle/db/bin

In addition, I can't change JAVA_HOME. I don't know where it is set I don't have JAVA_HOME set anywhere, I checked /etc/environment , ~/.bashrc and ~/.profile

Okay Update, Regarding JAVA_HOME. I found out where it was set (etc/profile) and I unset it and sourced. Not sure how it got set there, maybe a program did it.

What I want and expect is to switch between java versions and be able to compile and execute java programs.

Example, when I run a higher version of a java program from terminal.

Exception in thread "main" java.lang.UnsupportedClassVersionError: Program has been compiled by a more recent version of the Java Runtime (class file version 54.0), this version of the Java Runtime only recognizes class file versions up to 52.0

Finally for update-java-alternatives --list I have:

update-java-alternatives --list
java-12-oracle                 1091       /usr/lib/jvm/java-12-oracle
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64

Also If I try to set it:

sudo update-java-alternatives -s java-12-oracle
update-alternatives: error: no alternatives for jaccessinspector
update-alternatives: error: no alternatives for jaccesswalker
update-alternatives: error: no alternatives for kinit
update-alternatives: error: no alternatives for klist
update-alternatives: error: no alternatives for ktab
update-alternatives: error: no alternatives for mozilla-javaplugin.so
update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-12-oracle/bin/jaccessinspector
update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-12-oracle/bin/jaccesswalker
update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-12-oracle/bin/kinit
update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-12-oracle/bin/klist
update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-12-oracle/bin/ktab
  • The issue for the most part is now resolved. The main problem was JAVA_HOME variable in /etc/profile . Once I removed it and restarted the computer I was able to switch between the java versions. Now I need to figure out how to auto update JAVA_HOME variable after switching versions. – ConnectionistSystems Jul 23 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.