alternatives java 多版本切换

张映 发表于 2019-11-28

分类目录: java/android

标签:,

用yum安装了openjdk,然后又想用oracle jdk。原来的openjdk,又不想丢,怎么办呢?

1,已有openjdk

[root@localhost ~]# alternatives --config java

共有 1 个提供“java”的程序。

  选项    命令
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre/bin/java)

按 Enter 保留当前选项[+],或者键入选项编号:

[root@localhost jvm]# env |grep -i java
OLDPWD=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
CLASSPATH=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/lib:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/lib/dt.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/lib/tools.jar

2,安装oracle jdk1.8

下载:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

jdk-8u231-linux-x64.tar.gz,这是二进制包,下来了就能用

# tar zxvf jdk-8u231-linux-x64.tar.gz
# mv jdk1.8.0_231 /usr/local/

3,用alternatives将java加入多版本


语法:alternatives --install <link> <name> <path> <priority>

参数:
install表示安装
link是符号链接
name则是标识符
path是执行文件的路径
priority则表示优先级

# alternatives --install /usr/bin/java java /usr/local/jdk1.8.0_231/bin/java 2

# alternatives --config java

共有 2 个提供“java”的程序。

  选项    命令
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre/bin/java)
   2           /usr/local/jdk1.8.0_231/bin/java

按 Enter 保留当前选项[+],或者键入选项编号:2  //选择2

修改环境变量:

# export JAVA_HOME=/usr/local/jdk1.8.0_231
# export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[root@localhost ~]# java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)

4,恢复到openjdk1.8

[root@localhost ~]# alternatives --config java

共有 2 个提供“java”的程序。

  选项    命令
-----------------------------------------------
*  1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64/jre/bin/java)
 + 2           /usr/local/jdk1.8.0_231/bin/java

按 Enter 保留当前选项[+],或者键入选项编号:1

# export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.201.b09-2.el7_6.x86_64
# export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[root@localhost ~]# java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)

 



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/android/2278.html

留下评论

留下评论
  • (必需)
  • (必需) (will not be published)
  • (必需)   9X3=?