在Ubuntu中通过update-alternatives切换软件版本

前端之家收集整理的这篇文章主要介绍了在Ubuntu中通过update-alternatives切换软件版本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令、哪个软件版本,比如,我们在系统中同时安装了open jdk和sun jdk两个版本,而我们又希望系统默认使用的是sun jdk,那怎么办呢?通过update-alternatives就可以很方便的实现了。

sudo update-alternatives --install /usr/bin/java java /opt/java/jdk1.6.0_27/bin/java 1062

其中1062 为alternative更改后的优先级,新更改的优先级需要大于当前的,通过update-alternatives --display java 可以查看。

现在介绍下 update-alternatives 命令中的几个主要选项,diplay、install、remove、config;

1、display选项的功能就是查看一个命令链接组的所有信息,包括链接的模式(自动还是手动)、链接priority值、所有可 用的链接命令等等。使用语法:update-alternatives --display name,其中name为命令链接名称,如java,执行命令: update-alternatives --display java 将会出现类似下面的结果

java - auto mode

link currently points to /opt/java/jdk1.6.0_27/bin/java

/opt/java/jdk1.6.0_27/bin/java - priority 1062

/usr/lib/jvm/java-6-openjdk/jre/bin/java - priority 1061

slave java.1.gz: /usr/lib/jvm/java-6-openjdk/jre/man/man1/java.1.gz

Current 'best' version is '/opt/java/jdk1.6.0_27/bin/java'.

2、install选项的功能就是增加一组新的系统命令链接符了,使用语法为:update-alternatives --install link name path priority [--slave link name path]... 其中link为系统中功能相同软件的公共链接目录,比如/usr/bin/java(需绝对目录); name为命令链接名称,如java; path为你所要使用新命令、新软件的所在目录; priority为优先级,当命令链接已存在时,需高于当前值,因为当alternative为自动模式时,系统默认启用priority高的链接; --slave为从alternative。

alternative有两种模式:auto和manual,默认都为auto模式,因为大多数情况下update-alternatives命令都被postinst (configure) or prerm (install)调用的,如果将其更改成手动的话安装脚本将不会更新它了。

3、remove选项的功能删除一个alternative及相关从alternative,使用语法为:update-alternatives --remove name path,其中name与path与install中的一致,如果所删除链接组中还有其他链接的话,系统将会自动从其他中选择一个priority高的链接作为默认为链接

4、config选项功能为在现有的命令链接选择一个作为系统默认的,使用语法为:update-alternatives --config name,如执行:sudo update-alternatives --config java,将会有类似以下信息:

There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status

------------------------------------------------------------

* 0 /opt/java/jdk1.6.0_27/bin/java 1062 auto mode

1 /opt/java/jdk1.6.0_27/bin/java 1062 manual mode

2 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode

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

若是想将manual改回自动模式的话,执行命令: update-alternatives --auto name即可,如

update-alternatives --config java



Installing the JDK

The instructions on the AOSP page are out of date as of 9/13/2013; the apt repository they recommend has no package called sun-java6-jdk. The following manual recipe should install the proper JDK and replace the OpenJDK installed as a dependency of maven above.

Install Oracle JDK 6 (required by Kitkat and older AOSP versions)

$ sudo add-apt-repository ppa:webupd8team/java

$ sudo apt-get update

$ sudo apt-get install oracle-java6-installer

If you cannot install JDK 1.6 try below alternative steps.

Alternative(Install JDK 1.6):
## download java jdk from http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-419409.html#jdk-6u38-oth-JPR
## install java
$ chmod +x jdk-6u38-linux-x64.bin
$ sudo ./jdk-6u38-linux-x64.bin
$ sudo mv jdk1.6.0_38 /usr/lib/jvm/

$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_38/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_38/bin/javac 1
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_38/bin/javaws 1


## when prompted select the 6.0_38 versions of each
$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac
$ sudo update-alternatives --config javaws

$ cd /usr/lib/jvm/
$ sudo ln -s ./jdk1.6.0_38/ java-6-sun

##Setting JAVA_HOME/Path for Maven and jarsigner
##For all users:
$ sudo nano /etc/profile.d/javaenv.sh
----- Contents -----
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin

----- End of contents -----
$ sudo chmod a+x /etc/profile.d/javaenv.sh

Install OpenJDK 7 (required by Lollipop and newer AOSP versions)

$ sudo apt-get install openjdk-7-jdk

Post check JDK version

to list currently installed jdk versions:

$ sudo update-java-alternatives -l

java-1.7.0-openjdk-amd64 1051 /usr/lib/jvm/java-1.7.0-openjdk-amd64

java-6-oracle 1 /usr/lib/jvm/java-6-oracle

select oracle JDK 6 as default (Lollipop would set JAVA_HOME to use OpenJDK7 when doing "lunch")

$ sudo update-java-alternatives java-6-oracle

create asymbolic link for JDK6

$ sudo ln -s /usr/lib/jvm/java-6-oracle /usr/lib/jvm/java-6-sun

Reboot required for it to propagate to all users.

原文链接:https://www.f2er.com/ubuntu/354696.html

猜你在找的Ubuntu相关文章