本文介绍在Vagrant搭建的CentOS安装OpenJDK。
1. 启动虚拟机
D:\sandbox\code\demo>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
2. ssh登录虚拟机
D:\sandbox\code\demo>vagrant ssh
Last login: Tue Nov 9 14:15:16 2021 from 10.0.2.2
Last login: Tue Nov 9 14:15:16 2021 from 10.0.2.2
[vagrant@localhost ~]$
3. 安装OpenJDK 11
[vagrant@localhost ~]$ sudo yum install java-11-openjdk-devel
4. 确认Java安装成功
[vagrant@localhost ~]$ java -version
openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing)
5. 安装OpenJDK 8
[vagrant@localhost ~]$ sudo yum install java-1.8.0-openjdk-devel
6. 虽然安装了两个版本的JDK,但是默认JDK是11
[vagrant@localhost ~]$ java -version
openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing)
7. 如果要切换默认JDK的版本,可以
[vagrant@localhost ~]$ sudo alternatives --config java
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
+ 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64/bin/java)
* 2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre/bin/java)
Enter to keep the current selection[+], or type selection number:
8. 如果要卸载JDK
sudo yum remove java-1.8.0-openjdk-devel
参考
How to Install Java on CentOS 7
|