问题描述
有没有办法使用update-alternatives
快速切换所有与Java相关的替代方案?
例如,如果要将Java切换到7,我运行sudo update-alternatives --config java
并选择Java 7 OpenJdk。但如果我运行update-alternatives --get-selections | grep java
,我会得到以下信息,
appletviewer auto /usr/lib/jvm/java-6-openjdk-amd64/bin/appletviewer
extcheck auto /usr/lib/jvm/java-6-openjdk-amd64/bin/extcheck
idlj auto /usr/lib/jvm/java-6-openjdk-amd64/bin/idlj
itweb-settings auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/itweb-settings
jar auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jar
jarsigner auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jarsigner
java manual /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java
javac auto /usr/lib/jvm/java-6-openjdk-amd64/bin/javac
javadoc auto /usr/lib/jvm/java-6-openjdk-amd64/bin/javadoc
javah auto /usr/lib/jvm/java-6-openjdk-amd64/bin/javah
javap auto /usr/lib/jvm/java-6-openjdk-amd64/bin/javap
javaws auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/javaws
jconsole auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jconsole
jdb auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jdb
jexec auto /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/jexec
jhat auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jhat
jinfo auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jinfo
jmap auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jmap
jps auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jps
jrunscript auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jrunscript
jsadebugd auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jsadebugd
jstack auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jstack
jstat auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jstat
jstatd auto /usr/lib/jvm/java-6-openjdk-amd64/bin/jstatd
keytool auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/keytool
native2ascii auto /usr/lib/jvm/java-6-openjdk-amd64/bin/native2ascii
orbd auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/orbd
pack200 auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/pack200
policytool auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/policytool
rmic auto /usr/lib/jvm/java-6-openjdk-amd64/bin/rmic
rmid auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/rmid
rmiregistry auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/rmiregistry
schemagen auto /usr/lib/jvm/java-6-openjdk-amd64/bin/schemagen
serialver auto /usr/lib/jvm/java-6-openjdk-amd64/bin/serialver
servertool auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/servertool
tnameserv auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/tnameserv
unpack200 auto /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/unpack200
wsgen auto /usr/lib/jvm/java-6-openjdk-amd64/bin/wsgen
wsimport auto /usr/lib/jvm/java-6-openjdk-amd64/bin/wsimport
xjc auto /usr/lib/jvm/java-6-openjdk-amd64/bin/xjc
正如您所看到的,我的Java替代方案已切换到7,但是基于OpenJDK 6的其他替代方案都没有切换。当然我可以手动切换每个人或写一个脚本来做到这一点,但我认为有一个更好的方法来实现这一目标。
最佳解决思路
诀窍是使用update-java-alternatives
(来自java-common
包)。
因此,为了获得OpenJDK 6到7,我使用update-java-alternatives -l
获取Java备选列表,然后将sudo update-java-alternatives -s java-1.7.0-openjdk-amd64
用于JDK。
注意:上面的命令引发了以下错误,
update-alternatives: error: no alternatives for mozilla-javaplugin.so.
update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so
这是因为默认情况下未安装openjdk插件。修复运行
sudo apt-get install icedtea-7-plugin
并重新运行update-java-alternatives
。
次佳解决思路
jbro在http://ubuntuforums.org/showthread.php?t=1817528问了同样的问题,为这个问题写了一个脚本,
#!/bin/bash
update-alternatives --get-selections | grep -i openjdk |
while read line
do
alternative=$(echo $line | awk '{print $1}')
path=$(echo $line | awk '{print $3}')
newpath=$(echo $path | sed -e 's/java-6-openjdk/java-6-sun/')
status=unchanged
if [ -f $newpath ]
then
status=modified
echo "-> update-alternatives --set $alternative $newpath"
update-alternatives --set $alternative $newpath
else
echo "$alternative unchanged"
fi
done
如果没有更好的答案,这似乎是一个可靠的解决方法,但我认为update-alternatives
必须有更好的方法来处理这个问题。
我编辑了一些代码,因为它不允许您在安装之前进行更改。我添加了两行如下……
#!/bin/bash
update-alternatives --get-selections | grep -i openjdk |
while read line
do
alternative=$(echo $line | awk '{print $1}')
path=$(echo $line | awk '{print $3}')
newpath=$(echo $path | sed -e 's/java-6-openjdk/java-6-sun/')
status=unchanged
if [ -f $newpath ]
then
status=modified
echo "-> update-alternatives --install /usr/bin/$alternative $alternative $newpath 1"
update-alternatives --install /usr/bin/$alternative $alternative $newpath 1
echo "-> update-alternatives --set $alternative $newpath"
update-alternatives --set $alternative $newpath
else
echo "$alternative unchanged"
fi
done