MAC system multi-version JDK installation guide: allowing you to easily cope with different version requirements during development

1. Background

In the process of Java development, we may need to use different versions of JDK. For example: some old Java applications can only run on older versions of JDK, while some new Java applications require newer JDKs to run.

On a MAC system, how to install multiple versions of JDK, configure environment variables, and quickly switch to the required version? This article will demonstrate using JDK1.8 and JDK11 as examples.

2. Install multiple versions of JDK

If you are not familiar with the installation and download of JDK, you can refer to my other article " Quickly complete the MAC system JDK installation and environment variable configuration to make your development road smoother ", download and install JDK1.8 and JDK11, for subsequent use.

Open a terminal window and execute the following command to view the JDK version we installed

# 访问 JDK 安装目录
cd /Library/Java/JavaVirtualMachines

# 查看安装的 JDK 版本
ls -al

The query information is as follows:

woniu@MacBook-Pro ~ % cd /Library/Java/JavaVirtualMachines
woniu@MacBook-Pro JavaVirtualMachines % ls -al
total 0
drwxr-xr-x  5 root  wheel  160  2 17 15:43 .
drwxr-xr-x  4 root  wheel  128  8 16  2022 ..
drwxr-xr-x  3 root  wheel   96  2 17 15:43 jdk-11.0.16.jdk
drwxr-xr-x  3 root  wheel   96  8 16  2022 jdk1.8.0_192.jdk
drwxr-xr-x  3 root  wheel   96  8 19  2022 jdk1.8.0_281.jdk

You can see that there are three JDK versions on my computer, JDK 11 and two JDK1.8

3. Environment variable configuration

3.1. Open the environment variable file

In the terminal window, execute the following command:

# 进入当前用户的 home 目录
cd /Users/修改为自己 MAC 电脑用户名称

# 输入
cd ~

# 打开环境变量配置文件
open .bash_profile

# 报错:.bash_profile does not exist.
# 第一次配置环境变量,先创建文件
touch .bash_profile

# 再次执行打开环境变量配置文件
open .bash_profile

3.2. Configure JDK multi-version environment variables

# 复制如下内容,JAVA_HOME 替换为自己目录
export JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
export JAVA_11_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.16.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH:.

alias jdk8='export JAVA_HOME=$JAVA_8_HOME'
alias jdk11='export JAVA_HOME=$JAVA_11_HOME'

3.3. Check environment variables

# 配置文件立即生效
source .bash_profile

# 查看 JAVA_HOME 目录
echo $JAVA_HOME

# 查看 JDK 版本信息
java -version

4. JDK version switching

We have defined two aliases: jdk8 and jdk11, with the default configuration being jdk8.

To switch to jdk11, enter the command jdk11 in the terminal as follows:

woniu@MacBook-Pro ~ % jdk11
woniu@MacBook-Pro ~ % java -version
java version "11.0.16" 2022-07-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.16+11-LTS-199)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.16+11-LTS-199, mixed mode)

To switch back to jdk8, enter the command jdk8 in the terminal as follows:

woniu@MacBook-Pro ~ % jdk8
woniu@MacBook-Pro ~ % java -version
java version "1.8.0_281"
Java(TM) SE Runtime Environment (build 1.8.0_281-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.281-b09, mixed mode)

This tutorial ends here, friends can happily start their programming journey.

Guess you like

Origin blog.csdn.net/u011374856/article/details/129087707