一键脚本
1
2
3
4
5
6
7
8
| cd /tmp && wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz && \
tar -xzvf jdk-21_linux-x64_bin.tar.gz -C /opt/ && \
sudo sed -i '1i export JAVA_HOME=/opt/jdk-21.0.5/' /etc/profile && \
sudo sed -i '2i export JRE_HOME=${JAVA_HOME}/jre' /etc/profile && \
sudo sed -i '3i export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib' /etc/profile && \
sudo sed -i '4i export PATH=${JAVA_HOME}/bin:$PATH' /etc/profile && \
source /etc/profile && \
java -version
|
安装部署
安装包安装
官网:https://www.oracle.com/java/technologies/downloads/#jdk21-linux
下载安装包:
1
| wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
|
1
| tar -xzvf jdk-21_linux-x64_bin.tar.gz -C /opt/
|
配置环境变量:
1
2
3
4
| sudo sed -i '1i export JAVA_HOME=/opt/jdk-21.0.5/' /etc/profile
sudo sed -i '2i export JRE_HOME=${JAVA_HOME}/jre' /etc/profile
sudo sed -i '3i export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib' /etc/profile
sudo sed -i '4i export PATH=${JAVA_HOME}/bin:$PATH' /etc/profile
|
dnf安装
选择一个版本进行安装,我选择安装devel
1
| dnf install openjdk-devel.x86_64 -y
|
查看版本
java version “23.0.1” 2024-10-15
Java(TM) SE Runtime Environment (build 23.0.1+11-39)
Java HotSpot(TM) 64-Bit Server VM (build 23.0.1+11-39, mixed mode, sharing)
切换版本
如果安装了多个版本的话,可以通过下面命令切换版本,或者直接在环境变量中指定。
1
| sudo alternatives --config java
|