Download the corresponding jdk version in oracle:
oracle official website download
After the download is complete, install the dmg file. After the installation is complete, command+space to bring up the search interface, and enter ter to enter the command line interface:
Check the current jdk version:
java -version
View the location of the current jdk installation and modify the configuration file:
/usr/libexec/java_home -V
Edit configuration file:
vi ~/.bash_profile
Insert the following content and modify the first line according to the current jdk installation location:
#jdk1.8
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_321.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH:.
Refresh the configuration:
source ~/.bash_profile