8

to try out the new features of jdk11 I installed it into /opt/ and want to use it using update-alternatives, like so:

sudo update-alternatives --install /etc/alternatives/java OpenJDK-11 /opt/jdk-11/bin/java 1102

which returns:

update-alternatives: using /opt/jdk-11/bin/javac to provide /etc/alternatives/java (OpenJDK-11) in auto mode

However, when I do

$ sudo update-alternatives --config java

I don't see the jdk I just installed. Instead I get a warning and the already existing choices:

update-alternatives: warning: /etc/alternatives/java has been changed (manually or by a script); switching to manual updates only
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      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

What does the warning mean, and why is it in fact showing both auto and manual mode entries? And why is my jdk11 not in there?

5
9

Your first command was wrong, I think; it should have been:

sudo update-alternatives /usr/bin/java java /opt/jdk-11/bin/java 1102

and the same for all the others executables of the jdk.

how to do it

Install jdk-11

First install jdk-11 inside a directory in /usr/lib/jvm. We will use openjdk-11+28-amd64 here

Create a shell script to udpate alternatives automatically

I call it jdk-11.sh

#!/bin/bash
if [[ $# -eq 0 ]] ;then
    echo "Usage: jdk-11.sh [-i|-r]"
    echo "  -i install alternatives for jdk-11"
    echo "  -r remove alternatives for jdk-11"
    exit -1
fi

install_dir="openjdk-11+28-amd64"

cmd=(java jexec jjs keytool pack200 rmid rmiregistry unpack200 jaotc jarsigner jar javac javadoc javap jcmd jdb jdeprscan jdeps jhsdb jimage jinfo jlink jmap jmod jps jrunscript jshell jstack jstatd jstat rmic serialver jconsole)

if [[ "$1" == "-i" ]] ;then
    for c in ${cmd[@]}; do
        update-alternatives --install /usr/bin/$c $c /usr/lib/jvm/$install_dir/bin/$c 1102
    done
fi

if [[ "$1" == "-r" ]] ;then
    for c in ${cmd[@]}; do
        update-alternatives --remove $c /usr/lib/jvm/$install_dir/bin/$c
        update-alternatives --auto $c
    done
fi

Then make jdk-11.sh executable, and run

sudo ./jdk-11.sh -i

to add alternatives for the new jdk-11

Switch to this jdk-11

then switch to that alternative with the help of update-java-alternatives

We need a helper file called .openjdk-11+28.jinfo inside /usr/lib/jvm

name=openjdk-11+28-amd64
alias=openjdk-11+28-amd64
priority=1102
section=main

hl rmid /usr/lib/jvm/openjdk-11+28-amd64/bin/rmid
hl java /usr/lib/jvm/openjdk-11+28-amd64/bin/java
hl keytool /usr/lib/jvm/openjdk-11+28-amd64/bin/keytool
hl jjs /usr/lib/jvm/openjdk-11+28-amd64/bin/jjs
hl pack200 /usr/lib/jvm/openjdk-11+28-amd64/bin/pack200
hl rmiregistry /usr/lib/jvm/openjdk-11+28-amd64/bin/rmiregistry
hl unpack200 /usr/lib/jvm/openjdk-11+28-amd64/bin/unpack200
hl jexec /usr/lib/jvm/openjdk-11+28-amd64/lib/jexec
jdkhl jlink /usr/lib/jvm/openjdk-11+28-amd64/bin/jlink
jdkhl jdeps /usr/lib/jvm/openjdk-11+28-amd64/bin/jdeps
jdkhl rmic /usr/lib/jvm/openjdk-11+28-amd64/bin/rmic
jdkhl jinfo /usr/lib/jvm/openjdk-11+28-amd64/bin/jinfo
jdkhl jstat /usr/lib/jvm/openjdk-11+28-amd64/bin/jstat
jdkhl javac /usr/lib/jvm/openjdk-11+28-amd64/bin/javac
jdkhl jhsdb /usr/lib/jvm/openjdk-11+28-amd64/bin/jhsdb
jdkhl jps /usr/lib/jvm/openjdk-11+28-amd64/bin/jps
jdkhl jstack /usr/lib/jvm/openjdk-11+28-amd64/bin/jstack
jdkhl jrunscript /usr/lib/jvm/openjdk-11+28-amd64/bin/jrunscript
jdkhl javadoc /usr/lib/jvm/openjdk-11+28-amd64/bin/javadoc
jdkhl jmod /usr/lib/jvm/openjdk-11+28-amd64/bin/jmod
jdkhl javap /usr/lib/jvm/openjdk-11+28-amd64/bin/javap
jdkhl jar /usr/lib/jvm/openjdk-11+28-amd64/bin/jar
jdkhl jaotc /usr/lib/jvm/openjdk-11+28-amd64/bin/jaotc
jdkhl jshell /usr/lib/jvm/openjdk-11+28-amd64/bin/jshell
jdkhl jmap /usr/lib/jvm/openjdk-11+28-amd64/bin/jmap
jdkhl jdeprscan /usr/lib/jvm/openjdk-11+28-amd64/bin/jdeprscan
jdkhl jimage /usr/lib/jvm/openjdk-11+28-amd64/bin/jimage
jdkhl jstatd /usr/lib/jvm/openjdk-11+28-amd64/bin/jstatd
jdkhl jdb /usr/lib/jvm/openjdk-11+28-amd64/bin/jdb
jdkhl serialver /usr/lib/jvm/openjdk-11+28-amd64/bin/serialver
jdkhl jcmd /usr/lib/jvm/openjdk-11+28-amd64/bin/jcmd
jdkhl jarsigner /usr/lib/jvm/openjdk-11+28-amd64/bin/jarsigner
jdk jconsole /usr/lib/jvm/openjdk-11+28-amd64/bin/jconsole

Then run

sudo update-java-alternatives -s openjdk-11+28-amd64

To roll-back to ubuntu package

sudo update-java-alternatives -s java-1.11.0-openjdk-amd64
sudo ./jdk-11.sh -r # optionnal
sudo rm -rf /usr/lib/jvm/openjdk-11+28-amd64 # optionnal
4
  • That makes a lot clear and thanks for all the effort. However, if we're creating all the symlinks one by one in the script, why do we also need the jinfo file? – Mr. Wrong Sep 29 '18 at 8:44
  • I guess one can see the jinfo file and update-java-alternatives as optionnal after all. But that way, you could switch back and forth to ubuntu package more easily. Don't forget to mark this as an answser above if you think so. – solsTiCe Sep 29 '18 at 11:09
  • I would like to mark this anwer as accepted, but more is wrong in my system (/etc/alternatives/java is a symlink to itself) and I need to find out more about that before I can try it out. – Mr. Wrong Oct 1 '18 at 6:56
  • It's because you run the wrong command first. This has nothing to do with what I told you. Delete the symlink and recreate it to point to /usr/lib/jvm/java-11-openjdk-amd64/bin/java – solsTiCe Oct 1 '18 at 7:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.