Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command update-alternatives --config java change nothing

Tags:

java

I want to switch my java version with the command update-alternatives --config java, but I don't see any changes after that. I'm using that command and have this output:

$ 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-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number: 1

Before it I have used Java 8 and selection was set on /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java. Then I switched, run java -version and have the same version as it was - Java 8:

$ java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment Corretto-8.212.04.1 (build 1.8.0_212-b04)
OpenJDK 64-Bit Server VM Corretto-8.212.04.1 (build 25.212-b04, mixed mode)

I changed JAVA_HOME and PATH to use Java 11, but it still changed nothing:

$ echo $PATH
/home/user9679/.sdkman/candidates/java/current/bin:/home/user9679/.sdkman/candidates/gradle
/current/bin:/home/user9679/.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-1.11.0-openjdk-amd64/

$ echo $JAVA_HOME
/usr/lib/jvm/java-1.11.0-openjdk-amd64/
like image 929
Orbit419 Avatar asked Jun 29 '26 04:06

Orbit419


1 Answers

Probably you have made a change in your system that prevent update-alternatives to work.


Expected behavior

As I know, in a typical installation, the java command is a symbolic link in /usr/bin/java. You must exclude the "real" java path from the $PATH variable. If your computer use a java command in a different directory, the update-alternatives will not work.

-- location of the java command
$ type -p java
/usr/bin/java

-- real location of the java command
$ readlink -f /usr/bin/java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java 

If you get a different result for the type -p java command, you must check if the location of the java command is determined by some folder in the $PATH and remove that part.


NOTE: You may define the JAVA_HOME variable processing the /usr/bin/java symbolic link.

-- real location of the JAVA_PATH variable
$ dirname /usr/lib/jvm/java-11-openjdk-amd64/bin/java 
/usr/lib/jvm/java-11-openjdk-amd64/bin/

$ dirname /usr/lib/jvm/java-11-openjdk-amd64/bin/
/usr/lib/jvm/java-11-openjdk-amd64/

-- setting the JAVA_HOME variable
$ export JAVA_HOME=`type -p java|xargs readlink -f|xargs dirname|xargs dirname`
like image 140
Jaime Avatar answered Jun 30 '26 17:06

Jaime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!