ONEKEY——安装JDK 17
# 1、环境准备
提示
- 支持系统:主流 Linux 发行版(Rocky、CentOS、Ubuntu、Debian 等)
- 权限需求:需 root 或 sudo 权限
- 网络:需访问清华大学开源镜像,推荐国内云主机/服务器
# 2、JDK 17 国内下载地址
警告
因为本文档与官网的版本可能有差异,需要自行访问官方,然后更换最新版本。地址如下
镜像源 | 版本 | 下载链接 |
---|---|---|
清华大学 | OpenJDK 17 | https://mirrors.tuna.tsinghua.edu.cn/Adoptium/17/jdk/x64/linux/ |
阿里云、华为云目前均无 OpenJDK 17 包,清华大学镜像为最优选择。只推荐清华源
# 3、解压并安装 JDK 17
#!/bin/bash
JDK_FILE="/opt/modules/OpenJDK17U-jdk_x64_linux_hotspot_17.0.15_6.tar.gz"
JDK_HOME="/opt/modules/jdk-17.0.15"
# 目录准备
mkdir -p /opt/modules
# 下载(如无则自动拉取)
if [ ! -f "$JDK_FILE" ]; then
echo "正在下载 JDK 17 ..."
curl -L -o "$JDK_FILE" "https://mirrors.tuna.tsinghua.edu.cn/Adoptium/17/jdk/x64/linux/OpenJDK17U-jdk_x64_linux_hotspot_17.0.15_6.tar.gz"
else
echo "已存在安装包:$JDK_FILE"
fi
# 解压(strip-components=1 目录扁平化,避免多重目录层)
if [ ! -d "$JDK_HOME" ]; then
mkdir -p "$JDK_HOME"
tar -zxvf "$JDK_FILE" -C "$JDK_HOME" --strip-components=1
else
echo "已存在解压目录:$JDK_HOME"
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
笔记
脚本自动判断文件/目录是否存在,可重复执行,便于批量部署。
# 4、环境变量配置
将 JAVA_HOME 与 PATH 写入 /etc/profile
,保证全局可用:
#!/bin/bash
if ! grep -q "jdk-17.0.15" /etc/profile; then
echo "export JAVA_HOME=/opt/modules/jdk-17.0.15" | sudo tee -a /etc/profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' | sudo tee -a /etc/profile
fi
source /etc/profile
echo "JAVA_HOME 已设置为:$JAVA_HOME"
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
如需局部用户可写入 ~/.bashrc
,方法一致。
# 5、alternatives 多版本管理
如果服务器存在多个 Java 版本,务必用 update-alternatives
统一切换:
sudo update-alternatives --install /usr/bin/java java /opt/modules/jdk-17.0.15/bin/java 2
sudo update-alternatives --install /usr/bin/javac javac /opt/modules/jdk-17.0.15/bin/javac 2
# 可选:一键切换为默认
sudo update-alternatives --set java /opt/modules/jdk-17.0.15/bin/java
sudo update-alternatives --set javac /opt/modules/jdk-17.0.15/bin/javac
1
2
3
4
5
6
2
3
4
5
6
注意
如遇 java -version
输出异常,建议优先检查 alternatives 配置是否指向正确 JDK。
# 6、一键自动化脚本
下面是一键全自动安装脚本,可复制保存为 install_jdk17.sh
,适配绝大部分 Linux 服务器:
#!/bin/bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: JaneTTR
set -ex
########### JDK 版本参数区 ############
JDK_VERSION="17.0.15_6"
JDK_SHORT="17.0.15"
JDK_FILENAME="OpenJDK17U-jdk_x64_linux_hotspot_${JDK_VERSION}.tar.gz"
JDK_DOWNLOAD_URL="https://mirrors.tuna.tsinghua.edu.cn/Adoptium/17/jdk/x64/linux/${JDK_FILENAME}"
JDK_INSTALL_DIR="/opt/modules/jdk-${JDK_SHORT}"
JDK_FILE_PATH="/opt/modules/${JDK_FILENAME}"
###########JDK_INIT start
echo "############## SETUP JDK_INIT start #############"
# 解压缩函数
extract_tar_gz() {
local file_path=$1
local dest_dir=$2
echo "Extracting file $file_path to directory $dest_dir..."
mkdir -p "$dest_dir"
tar -zxvf "$file_path" -C "$dest_dir" --strip-components=1
if [ $? -eq 0 ]; then
echo "File extracted successfully: $dest_dir"
else
echo "File extraction failed"
exit 1
fi
}
# 配置 JAVA_HOME 函数
configure_java_home() {
if grep -q "^export JAVA_HOME=" /etc/profile; then
sudo sed -i "s#^export JAVA_HOME=.*#export JAVA_HOME=${JDK_INSTALL_DIR}#" /etc/profile
else
echo "export JAVA_HOME=${JDK_INSTALL_DIR}" | sudo tee -a /etc/profile
fi
if ! grep -q "^export PATH=.*\$JAVA_HOME/bin" /etc/profile; then
echo "export PATH=\$PATH:\$JAVA_HOME/bin" | sudo tee -a /etc/profile
fi
source /etc/profile
echo "JAVA_HOME is set to: $JAVA_HOME"
}
# 检查并下载 JDK 文件
check_and_download_jdk() {
if [ -f "$JDK_FILE_PATH" ]; then
echo "JDK file exists: $JDK_FILE_PATH"
else
echo "JDK file does not exist, downloading..."
mkdir -p "$(dirname "$JDK_FILE_PATH")"
curl -L -o "$JDK_FILE_PATH" "$JDK_DOWNLOAD_URL"
if [ $? -eq 0 ]; then
echo "JDK download success: $JDK_FILE_PATH"
extract_tar_gz "$JDK_FILE_PATH" "$JDK_INSTALL_DIR"
else
echo "JDK download failed!!"
exit 1
fi
fi
}
# 添加并配置 update-alternatives
configure_update_alternatives() {
sudo update-alternatives --install /usr/bin/java java ${JDK_INSTALL_DIR}/bin/java 2
sudo update-alternatives --install /usr/bin/javac javac ${JDK_INSTALL_DIR}/bin/javac 2
sudo update-alternatives --set java ${JDK_INSTALL_DIR}/bin/java
sudo update-alternatives --set javac ${JDK_INSTALL_DIR}/bin/javac
java -version
}
main() {
check_and_download_jdk
if [ -d "$JDK_INSTALL_DIR" ]; then
echo "JDK home exists: $JDK_INSTALL_DIR"
else
extract_tar_gz "$JDK_FILE_PATH" "$JDK_INSTALL_DIR"
fi
configure_java_home
configure_update_alternatives
}
main
###########JDK_INIT end
echo "############## SETUP JDK_INIT end #############"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
使用方法
- 保存脚本
install_jdk17.sh
- 执行
chmod +x install_jdk17.sh
- 运行
sudo ./install_jdk17.sh