update-alternatives命令

mac2022-07-01  5

快速复习:

增加:$sudo update-alternatives --install 程序生成的快捷方式的目标完整位置 程序名 程序其中一个版本的完整路径

配置:$sudo update-alternatives --config 程序名(如java)

删除:$sudo update-alternatives  --remove 程序名 其中不要版本的完整路径

如下:

$sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-openjdk-i386/bin/java 1071 $sudo update-alternatives --config java $sudo update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java

 

1,作用:

  在linux上可能有多个版本的gcc或g++,Debian 系列发行linux 用 sudo update-alternatives 命令可以设置一个版本当设置默认程序。

  多个版本的应用共用一个组名,如java,这个java 并不是一个真正的程序,它是一个快捷方式文件,配置默认程序,在 /etc/alternatives/ 下。

2,命令

在终端输入 update-alternatives --help

Usage: update-alternatives [<option> ...] <command> Commands: --install <link> <name> <path> <priority> [--slave <link> <name> <path>] ... add a group of alternatives to the system. --remove <name> <path> remove <path> from the <name> group alternative. --remove-all <name> remove <name> group from the alternatives system. --auto <name> switch the master link <name> to automatic mode. --display <name> display information about the <name> group. --query <name> machine parseable version of --display <name>. --list <name> display all targets of the <name> group. --get-selections list master alternative names and their status. --set-selections read alternative status from standard input. --config <name> show alternatives for the <name> group and ask the user to select which one to use. --set <name> <path> set <path> as alternative for <name>. --all call --config on all alternatives. <link> is the symlink pointing to /etc/alternatives/<name>. (e.g. /usr/bin/pager) <name> is the master name for this link group. (e.g. pager) <path> is the location of one of the alternative target files. (e.g. /usr/bin/less) <priority> is an integer; options with higher numbers have higher priority in automatic mode. Options: --altdir <directory> change the alternatives directory. --admindir <directory> change the administrative directory. --log <file> change the log file. --force allow replacing files with alternative links. --skip-auto skip prompt for alternatives correctly configured in automatic mode (relevant for --config only) --verbose verbose operation, more output. --quiet quiet operation, minimal output. --help show this help message. --version show the version.

其中4个很重要

$sudo update-alternatives --install 程序生成的快捷方式的完整位置 程序名 程序其中一个版本的完整路径 $sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-openjdk-i386/bin/java 1071

   会在/usr/bin 和 /etc/alternatives 下生成相应文件。

$sudo update-alternatives --remove 程序名 其中不要版本的完整路径,如: $sudo update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java $sudo update-alternatives --config 程序名 ,如: $sudo update-alternatives --config java 彻底删除 $sudo update-alternatives --remove-all java

会把/etc/alternatives/java也删掉

3,示例

以共存多个jdk为例:

注意:

  这种方法并不好,这种只能改一个程序的默认版本,如java,但是javac、javah等还是前一个版本的,要同样的操作java,javah,javac等才是完整的改jdk版本,改jdk版本用环境变量切换脚本比这个方法好。  参看 这里「linux 切换多个jdk脚本」。

现在系统中已经安装了openjdk7,同时又想与jdk8共存,按下方法操作:

第1步:安装jdk8

下载 jdk安装文件,解压并复制到 /usr/lib/jvm/ 下

第2步:把jdk8安装到java alternatives中

$sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_102/bin/java 2048

其中:

/usr/bin/java 是个快捷方式,它是java的生成的快捷方式的完整路径,这里把它生成的在/usr/bin/javajava 是多个java版本的组名/usr/lib/jvm/jdk1.8.0_102/bin/java 是其中jdk8版本java的完整路径。2048 是jdk8版本的优先级 ,当命令链接已存在时,需高于当前值,因为当alternative为自动模式时,系统默认启用priority高的链接

第3步:配置

$sudo update-alternatives --config java

选择其中一个就可以 了。

注意:如果添加错了,可以用--remove 删除

$sudo update-alternatives --remove java /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java

4,疑问

如果在 .profile或者.bashrc里配置了JAVA_HOME,并加入了PATH,那么

alternatives指定的和 .profile或者.bashrc 认优先?如:

export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_102 export PATH=$JAVA_HOME/bin:$PATH:. export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

这要看PATH路径先后,

export PATH=$JAVA_HOME/bin:$PATH:. 就是/usr/lib/jvm/jdk1.8.0_102/bin/java 先。 export PATH=$PATH:$JAVA_HOME/bin:. 就是/usr/bin/java先

 

转载于:https://www.cnblogs.com/sjjg/p/6033161.html

最新回复(0)