本文主要是介绍CentOS8安装更换JDK,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
CentOS8安装更换JDK
背景:在服务器上启动java项目报错,JDK问题。更换jdk。探索整理安装JDK的方法。一些常用系统命令。清华源下载。
文章目录
- CentOS8安装更换JDK
- 准备工作:查看本地相关信息
- 方式一:yum安装
- 搜索
- 安装
- 配置环境变量
- 方式二:官网下载压缩包安装
- 获取下载链接(官网或者[国内镜像清华](https://mirrors.tuna.tsinghua.edu.cn/Adoptium/) )
- 解压(即安装)
- 配置环境变量
准备工作:查看本地相关信息
#查看系统的硬件架构
(base) [root@Server ~]# uname -m
x86_64(base) [root@Servere ~]# uname -a
Linux Server-3401572e-0499-44a9-88bb-b2b2061f3c4e 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
(base) [root@Server ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
(base) [root@Server ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
BIOS Vendor ID: QEMU
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Gold 6161 CPU @ 2.20GHz
BIOS Model name: pc-i440fx-2.8
Stepping: 4
CPU MHz: 2200.000
BogoMIPS: 4400.00
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 1024K
L3 cache: 30976K
NUMA node0 CPU(s): 0-15
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 arat md_clear flush_l1d#查看当前jdk版本
(base) [root@Server ~]# java --version
openjdk 17.0.8 2023-07-18
OpenJDK Runtime Environment GraalVM CE 22.3.3 (build 17.0.8+7-jvmci-22.3-b22)
OpenJDK 64-Bit Server VM GraalVM CE 22.3.3 (build 17.0.8+7-jvmci-22.3-b22, mixed mode, sharing)#查看当前系统安装了哪些关于java的东西
(base) [root@Server ~]# rpm -qa | grep java
javapackages-filesystem-5.3.0-1.module_el8.0.0+11+5b8c10bd.noarch
java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64
java-11-openjdk-headless-11.0.13.0.8-4.el8_5.x86_64
tzdata-java-2021e-1.el8.noarch(base) [root@Server ~]# rpm -qa | grep jdk
java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64
java-11-openjdk-headless-11.0.13.0.8-4.el8_5.x86_64
copy-jdk-configs-4.0-2.el8.noarch(base) [root@Server ~]# yum list installed | grep java
java-11-openjdk.x86_64 1:11.0.13.0.8-4.el8_5 @appstream
java-11-openjdk-headless.x86_64 1:11.0.13.0.8-4.el8_5 @appstream
javapackages-filesystem.noarch 5.3.0-1.module_el8.0.0+11+5b8c10bd @appstream
tzdata-java.noarch 2021e-1.el8 @appstream
java配置环境变量可参考博客了解配置这些到底是干啥用的
(base) [root@Server ~]# vim ~/.bash_profile
#配置环境变量
#Java
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk/jdk-17.0.10+7 # JDK 17安装路径
export PATH=$JAVA_HOME/bin:$PATH
方式一:yum安装
分三步:搜索,安装,配置环境变量
优点:直接install就完事了!
缺点:版本较少,没办法满足需求!
搜索
(base) [root@Server ~]# yum search java | grep -i --color JDK
Last metadata expiration check: 3:00:04 ago on Mon 15 Apr 2024 12:39:17 PM CST.
......
java-17-openjdk.x86_64 : OpenJDK 17 Runtime Environment
java-17-openjdk-demo.x86_64 : OpenJDK 17 Demos
java-17-openjdk-devel.x86_64 : OpenJDK 17 Development Environment
java-17-openjdk-headless.x86_64 : OpenJDK 17 Headless Runtime Environment
java-17-openjdk-javadoc.x86_64 : OpenJDK 17 API documentation
java-17-openjdk-javadoc-zip.x86_64 : OpenJDK 17 API documentation compressed in a single archive
java-17-openjdk-jmods.x86_64 : JMods for OpenJDK 17
......
icedtea-web.x86_64 : Additional Java components for OpenJDK - Java browser plug-in and Web Start implementation
openjdk-asmtools.noarch : Set of tools used to assemble / disassemble proper and improper Java .class files
openjdk-asmtools7.noarch : Set of tools used to assemble / disassemble proper and improper Java .class files for JDK version 11 and lesser
安装
(base) [root@Server ~]# yum install java-17-openjdk.x86_64
Last metadata expiration check: 3:00:49 ago on Mon 15 Apr 2024 12:39:17 PM CST.
Dependencies resolved.
======================================================================================================================Package Architecture Version Repository Size
======================================================================================================================
Installing:java-17-openjdk x86_64 1:17.0.1.0.12-2.el8_5 appstream 244 k
Installing dependencies:java-17-openjdk-headless x86_64 1:17.0.1.0.12-2.el8_5 appstream 41 MTransaction Summary
======================================================================================================================
Install 2 PackagesTotal download size: 41 M
Installed size: 190 M
Is this ok [y/N]: y
Downloading Packages:
(1/2): java-17-openjdk-17.0.1.0.12-2.el8_5.x86_64.rpm 146 kB/s | 244 kB 00:01
(2/2): java-17-openjdk-headless-17.0.1.0.12-2.el8_5.x86_64.rpm 8.7 MB/s | 41 MB 00:04
----------------------------------------------------------------------------------------------------------------------
Total 8.7 MB/s | 41 MB 00:04
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionRunning scriptlet: java-17-openjdk-headless-1:17.0.1.0.12-2.el8_5.x86_64 1/1Preparing : 1/1Installing : java-17-openjdk-headless-1:17.0.1.0.12-2.el8_5.x86_64 1/2Running scriptlet: java-17-openjdk-headless-1:17.0.1.0.12-2.el8_5.x86_64 1/2Installing : java-17-openjdk-1:17.0.1.0.12-2.el8_5.x86_64 2/2Running scriptlet: java-17-openjdk-1:17.0.1.0.12-2.el8_5.x86_64 2/2Verifying : java-17-openjdk-1:17.0.1.0.12-2.el8_5.x86_64 1/2Verifying : java-17-openjdk-headless-1:17.0.1.0.12-2.el8_5.x86_64 2/2Installed:java-17-openjdk-1:17.0.1.0.12-2.el8_5.x86_64 java-17-openjdk-headless-1:17.0.1.0.12-2.el8_5.x86_64Complete!
配置环境变量
同最上面(vim时注意目录)yum安装默认目录为 /usr/lib/jvm
方式二:官网下载压缩包安装
获取下载链接(官网或者国内镜像清华 )
(base) [root@Server env]# wget https://mirrors.tuna.tsinghua.edu.cn/Adoptium/17/jdk/x64/linux/OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz
--2024-04-15 16:25:37-- https://mirrors.tuna.tsinghua.edu.cn/Adoptium/17/jdk/x64/linux/OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.15.130, 2402:f000:1:400::2
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.15.130|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 192205930 (183M) [application/octet-stream]
Saving to: ‘OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz’OpenJDK17U-jdk_x64_linux_hots 100%[=================================================>] 183.30M 11.4MB/s in 18s2024-04-15 16:25:55 (10.5 MB/s) - ‘OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz’ saved [192205930/192205930]
解压(即安装)
#报错
(base) [root@Server env]# sudo tar -xzvf OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz /usr/lib/jvm/java-17-openjdk
tar: /usr/lib/jvm/java-17-openjdk: Not found in archive
tar: Exiting with failure status due to previous errors
#以为是没创建目录
(base) [root@Server env]# mkdir /usr/lib/jvm/java-17-openjdk
#根本原因没有指定 -C 即解压目录
(base) [root@Server env]# sudo tar -xzvf OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz /usr/lib/jvm/java-17-openjdk
tar: /usr/lib/jvm/java-17-openjdk: Not found in archive
tar: Exiting with failure status due to previous errors
(base) [root@Server env]# sudo tar -xzvf OpenJDK17U-jdk_x64_linux_hotspot_17.0.10_7.tar.gz -C /usr/lib/jvm/java-17-openjdk
jdk-17.0.10+7/
jdk-17.0.10+7/bin/
jdk-17.0.10+7/bin/javap
jdk-17.0.10+7/bin/jar
...
....
jdk-17.0.10+7/conf/logging.properties
配置环境变量
(base) [root@Server env]# vim ~/.bash_profile
(base) [root@Server env]# source ~/.bash_profile
(base) [root@Server env]# java -version
openjdk version "17.0.10" 2024-01-16
OpenJDK Runtime Environment Temurin-17.0.10+7 (build 17.0.10+7)
OpenJDK 64-Bit Server VM Temurin-17.0.10+7 (build 17.0.10+7, mixed mode, sharing)
这篇关于CentOS8安装更换JDK的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!