欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Ubuntu 安装多版本JDK --使用update-alternatives 管理多个版本

程序员文章站 2024-03-26 09:57:53
...

一、下载JDK
到官网下载:Oracle JDK 官网
选择 *.tar.gz 对应自己系统的版本
我下载的是:jdk-8u144-linux-x64.tar.gz
jdk-7u80-linux-x64.tar.gz (老版本的JDK在Oracle 官网下载需要登录,在Windows 下用迅雷下载对应的JDK可以不用登录,不过下载后文件名字可能需要改,包括后缀名)
二、解压到指定目录
创建目录:

sudo mkdir /usr/lib/jvm

解压到刚才创建的目录:

sudo tar -zxvf jdk-8u144-linux-x64.tar.gz -C /usr/lib/jvm/

三、使用update-alternatives进行配置

 sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_144/bin/java 400
 sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0_144/bin/javac 400
 sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.8.0_144/bin/jar 400
 sudo update-alternatives --install /usr/bin/javah javah /usr/lib/jvm/jdk1.8.0_144/bin/javah 400
 sudo update-alternatives --install /usr/bin/javap javap /usr/lib/jvm/jdk1.8.0_144/bin/javap 400

初次安装JDK:

$sudo update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/jdk1.8.0_144/bin/java
Nothing to configure.

验证:

$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

JDK 1.7 那个版本的安装类似。
update-alternatives 设置不同于1.8的优先级。

toby@toby-ThinkPad:~/Downloads$ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_80/bin/java 300
toby@toby-ThinkPad:~/Downloads$ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_80/bin/javac 300
toby@toby-ThinkPad:~/Downloads$ sudo update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.7.0_80/bin/jar 300
toby@toby-ThinkPad:~/Downloads$ sudo update-alternatives --install /usr/bin/javah javah /usr/lib/jvm/jdk1.7.0_80/bin/javah 300
toby@toby-ThinkPad:~/Downloads$ sudo update-alternatives --install /usr/bin/javap javap /usr/lib/jvm/jdk1.7.0_80/bin/javap 300

经过上步配置,终端输入

toby@toby-ThinkPad:~/Downloads$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

使用 sudo update-alternatives –config name 在不同的Java版本之间切换:

[email protected]:~/Downloads$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/jdk1.8.0_144/bin/java   400       auto mode
  1            /usr/lib/jvm/jdk1.7.0_80/bin/java    300       manual mode
  2            /usr/lib/jvm/jdk1.8.0_144/bin/java   400       manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/lib/jvm/jdk1.7.0_80/bin/java to provide /usr/bin/java (java) in manual mode

切换后 /usr/bin/java java 就变成手动模式了。其他的不受影响。
此时,终端输入:

toby@toby-ThinkPad:~/Downloads$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
toby@toby-ThinkPad:~/Downloads$ javac -version
javac 1.8.0_144
toby@toby-ThinkPad:~/Downloads$ ll /etc/alternatives/java*
lrwxrwxrwx 1 root root 33 99 19:21 /etc/alternatives/java -> /usr/lib/jvm/jdk1.7.0_80/bin/java*
lrwxrwxrwx 1 root root 35 99 18:01 /etc/alternatives/javac -> /usr/lib/jvm/jdk1.8.0_144/bin/javac*
lrwxrwxrwx 1 root root 35 99 18:05 /etc/alternatives/javah -> /usr/lib/jvm/jdk1.8.0_144/bin/javah*
lrwxrwxrwx 1 root root 35 99 18:07 /etc/alternatives/javap -> /usr/lib/jvm/jdk1.8.0_144/bin/javap*

终端输入:

toby@toby-ThinkPad:~/Downloads$ sudo update-alternatives --auto java
update-alternatives: using /usr/lib/jvm/jdk1.8.0_144/bin/java to provide /usr/bin/java (java) in auto mode

把Java那项改为 自动模式。
此时再次查看:

toby@toby-ThinkPad:~/Downloads$ ll /etc/alternatives/java*
lrwxrwxrwx 1 root root 34 99 19:22 /etc/alternatives/java -> /usr/lib/jvm/jdk1.8.0_144/bin/java*
lrwxrwxrwx 1 root root 35 99 18:01 /etc/alternatives/javac -> /usr/lib/jvm/jdk1.8.0_144/bin/javac*
lrwxrwxrwx 1 root root 35 99 18:05 /etc/alternatives/javah -> /usr/lib/jvm/jdk1.8.0_144/bin/javah*
lrwxrwxrwx 1 root root 35 99 18:07 /etc/alternatives/javap -> /usr/lib/jvm/jdk1.8.0_144/bin/javap*

参考:
Ubuntu 安装 JDK 7 / JDK8 的两种方式

update-alternatives 使用参考:
update-alternatives详解
update-alternatives命令
update-alternatives命令