为了尝试 jdk11 的新功能,我将其安装到 /opt/ 中,并想使用 update-alternatives 使用它,如下所示:
sudo update-alternatives --install /etc/alternatives/java OpenJDK-11 /opt/jdk-11/bin/java 1102
返回:
update-alternatives: using /opt/jdk-11/bin/javac to provide /etc/alternatives/java (OpenJDK-11) in auto mode
然而,当我这样做的时候
$ sudo update-alternatives --config java
我没有看到刚刚安装的 jdk。相反,我收到一个警告和已经存在的选择:
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
这个警告是什么意思?为什么它实际上同时显示自动和手动模式条目?为什么我的 jdk11 不在其中?
答案1
我认为你的第一个命令是错误的;它应该是:
sudo update-alternatives /usr/bin/java java /opt/jdk-11/bin/java 1102
对于 jdk 的所有其他可执行文件来说也是如此。
怎么做
安装 jdk-11
首先在目录中安装 jdk-11 /usr/lib/jvm
。我们将openjdk-11+28-amd64
在这里使用
创建一个 shell 脚本来自动更新替代方案
我称之为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
然后使jdk-11.sh
可执行文件并运行
sudo ./jdk-11.sh -i
添加新 jdk-11 的替代方案
切换到这个jdk-11
然后切换到该替代方案,借助update-java-alternatives
.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
然后运行
sudo update-java-alternatives -s openjdk-11+28-amd64
回滚到 ubuntu 软件包
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