变更Linux下的Java版本 alternatives

时间:2023-03-10 02:56:26
变更Linux下的Java版本  alternatives

默认正常情况下,即使使用Java 1.6版本Java脚本jdk-6u31-linux-i586.bin,安装Java运行后,会出现自动升级为1.7版本状态的情况。针对某些应用程序需要基于1.6版本方可运行的情况,需要降级为1.6。具体的操作方法有两种:
1. 使用alternatives命令来实现。
alternatives是Linux下的一个功能强大的命令,只能在root权限下执行。如系统中有几个命令功能十分类似,却又不能随意删除,那么可以用 alternatives 来指定一个全局的设置。
[root@Excel7070 ~]# alternatives
alternatives version 1.3.49.3 - Copyright (C) 2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
usage: alternatives --install <link> <name> <path> <priority>
                    [--initscript <service>]
                    [--slave <link> <name> <path>]*
       alternatives --remove <name> <path>
       alternatives --auto <name>
       alternatives --config <name>
       alternatives --display <name>
       alternatives --set <name> <path>
common options: --verbose --test --help --usage --version
                --altdir <directory> --admindir <directory>
可以看到相应的alternatives的相关命令信息。
说明:
alternatives --install <link> <name> <path> <priority>
其中,
install表示安装
link是符号链接
name则是标识符
path是执行文件的路径
priority则表示优先级
在当前状态下,我需要修改java的版本,具体操作显示如下:
a. 在root中运行 alternatives --config java,具体显示如下:
[root@Excel7070 ~]# alternatives --config java
There are 2 programs which provide 'java'.
  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk/bin/java
   2           /usr/lib/jvm/jre-1.6.0-openjdk/bin/java
Enter to keep the current selection[+], or type selection number: 2
b. 然后就选择需要使用的版本号所在选项即可。"2"
c. 再运行java -version
看到的就是当前选择的java版本1.6了。
2.  使用安装修改的方式。这种方式运行之前发生有点异常,第一种方法可以保留与选择,更为合适。
已经安装了1.7版本java,需要如下操作:
chmod +x jdk-6u32-linux-i586.bin
安装1.6版本
./jdk-6u32-linux-i586.bin
mv jdk1.6.0_32/ /usr/java
cd /usr/java
rm -fr latest
ln -s jdk1.6.0_32/ latest
再修改/etc/profle里面的java路径信息即可。
完成profile修改后,source /etc/pofile
java -version就可以看到版本也降级了

#JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/
#JAVA_BIN=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.45.x86_64/bin
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.71-1.b15.el6_7.x86_64/jre/
JAVA_BIN=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.71-1.b15.el6_7.x86_64/jre/bin/

PATH=$PATH:$JAVA_BIN