This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
JDK download address
You can download the JDK from jdk.java.net/archive/
Install the JDK
Mac JDK is installed in a specified directory: / Library/Java/JavaVirtualMachines /, so it can be the view of their installation directory so the JDK.
3. Configure environment variables
The next step is to configure the environment variables and implement JDK version management. First, run the vim ~/.bash_profile command to modify the environment variables. MAC recommends modifying the environment variables here instead of /etc/profile.
The configuration files differ as follows:
- /etc/profile: used to set system-level environment variables and boot programs. Configuration in this file takes effect for all users. When the user logs in, the file is executed and the shell Settings are looked up from the configuration file in the /etc/profile.d directory. It is generally not recommended to add environment variables to the /etc/profile file because Settings added in this file will apply to all users.
- ~/. Bash_profile: valid only for a single user. The file is stored in ~/. Bash_profile. This file can also be used to configure environment variables and launchers, but only for a single user. Like profile files, bash_profile takes effect when the user logs in and can also be used to set environment variables. Unlike profiles, however, bash_profile only applies to the current user.
Export JAVA_8_HOME = / Library/Java/JavaVirtualMachines jdk1.8.0 _202. JDK/Contents/Home export JAVA_11_HOME = / Library/Java/JavaVirtualMachines/JDK – 11.0.2. JDK/Contents/Home PATH = JAVAHOME/bin:JAVA_HOME/bin:JAVAHOME/bin:PATH CLASSPATH= JAVAHOME/lib/tools.jar:JAVA_HOME/lib/tools.jar:JAVAHOME/lib/tools.jar:JAVA_HOME/lib/dt.jar export JAVA_HOME=JAVA_8_HOME alias jdk8=”export JAVA_HOME=JAVA_8_HOME” alias jdk11=”export JAVA_HOME=$JAVA_11_HOME” export PATH export CLASSPATH
After the configuration is complete, run source ~/.bash_profile for it to take effect.
Four, verification effect
Enter jdk8 on the terminal and use Java -version to verify the version.
Then enter jdk11 and enter Java -version to verify the version.
Solve the problem of closing the terminal and invalid configuration
Reasons for this problem:
The terminal used is ZSH, which loads the ~/.zshrc file, and the ‘.zshrc ‘file does not define the task environment variables
Solution:
1. Run the following command to open the. ZSHRC file:
open -e ~/.zshrc
Copy the code
Add a line to the end of the ~/.zshrc file:
source ~/.bash_profile
Copy the code
3. Run the command to make it take effect immediately
source ~/.zshrc
Copy the code
4. Create a new window, test again, and pass.