Ubuntu22.04安装多个版本jdk,切换jdk版本
不要在线安装openjdk,手动下载orcale_jdk下载地址,通常是:版本号_linux_x86_64_bin.tar.gz
#新建目录放jdk
sudo mkdir -p /usr/local/java
#解压下载的jdk
sudo tar -xvzf jdk-17_linux-x64_bin.tar.gz -C /usr/local/java
sudo tar -xvzf jdk-8u391-linux-x64.tar.gz -C /usr/local/java
sudo tar -xvzf jdk-11.0.22_linux-x64_bin.tar.gz -C /usr/local/java
#在 /etc/profile.d下新建一个jdk.sh的文件来配置jdk
sudo vim /etc/profile.d/jdk.sh
jdk.sh编辑内容:
#设置默认jdk17
export JAVA_HOME=/usr/local/java/ jdk-17.0.11
export PATH=$JAVA_HOME/bin:$PATH
#切换jdk设置
setJavaHome() {
export JAVA_HOME=$1
export PATH=$JAVA_HOME/bin:$PATH
}
保存退出jdk.sh
#给jdk.sh文件权限
sudo chmod +x /etc/profile.d/jdk.sh
source /etc/profile.d/jdk.sh
#查看jdk版本号,jdk-11.0.22 jdk-17.0.11 jdk1.8.0_391
ls /usr/local/java
#编辑~/.bashrc,设置别名
alias setjdk8= 'setJavaHome /usr/local/java/jdk1.8.0_391'
alias setjdk11='setJavaHome /usr/local/java/jdk-11.0.22'
alias setjdk17='setJavaHome /usr/local/java/jdk-17.0.11 '
source ~/.bashrc
默认是jdk17,以后用setjdk8,setjdk11,setjdk17就可以切换jdk了