“This is the second day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.
background
After upgrading to MacOS BigSur/Monterey one after another, two virtual machines (PD and VMWare Funison) were installed on the computer, and the daily development experience was not good. After a trade-off, back up the data, wipe the hard drive and install a dual-system MacBook, then reinstall the environment.
Its installation
OpenJDK is an open source version of Java that is built and maintained by Oracle as well as by other organizations or companies. OpenJDK builds from Oracle are selected.
Since September 2017, Oracle provides JDK releases under a free open source license (similar to that of Linux). Availability and community support of OpenJDK releases provided by Oracle is listed separately on jdk.java.net.
Since September 2017, Oracle has offered JDK versions under a free open source license (similar to Linux). The availability and community support for OpenJDK versions provided by Oracle are listed separately on jdk.java.net. Oracle Java SE Support Roadmap
Install Homebrew
Install OpenJDK with Homebrew for easy management (view, update, uninstall)
Execute the installation script on the terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Copy the code
Install its @ 8
Using the BREW command line tool:
brew install openjdk@8
Copy the code
Configure its @ 8
In order for Java wrappers to find the JDK, as prompted by the installation information in the output, the link needs to be set up manually:
# For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk
Copy the code
To configure the $JAVA_HOME environment variable, find one of the.bash_profile/.bashrc/.zshrc configuration files and add the following line:
exportJAVA_HOME = $(/ usr/libexec/JAVA_HOME - v1.8)Copy the code
Then exit terminal and open again, or reload configuration file:
# Take.zshrc for example
source ~/.zshrc
Copy the code
Use the Java command line tool in the terminal to check whether the configuration is effective:
java -version
Copy the code
If the version information is displayed, the configuration is successful:
openjdk version "1.8.0 comes with _312"OpenJDK Runtime Environment (build 1.8.0_312-BRE_2021_10_20_23_15-b00) mixed mode)Copy the code
JDK version switching (session level)
New projects should be using JDK@11 or JDK@17, but there are still many projects in the industry using JDK@8.
Jenv-manage Your Java Environment for practical use
Dynamically find Java Home
Use the /usr/libexec/java_HOME command line tool (supports dynamic lookup of Java Home, defaults to the latest JDK version of Java Home)
➜ ~ / usr/libexec/java_home/usr /local/ Cellar/its / 17.0.1 / libexec/its JDK/Contents/Home ➜ ~ / usr/libexec/java_home - v1.8 / usr /local/ Cellar/its @ 8/1.8.0 comes with + 312 / libexec/its JDK/Contents/Home ➜ ~ / usr/libexec/java_home -v Matching Java Virtual Those (3) : 17.0.1 (x86_64)"Homebrew" - "Its 17.0.1" /usr/local/ Cellar/its / 17.0.1 / libexec/its JDK/Contents/Home 11.0.12 (x86_64)"Homebrew" - "Its 11.0.12" /usr/local/ Cellar/its @ 11/11.0.12 / libexec/its JDK/Contents/Home 1.8.0 comes with _312 (x86_64)"Homebrew" - "OpenJDK 8" /usr/local/ Cellar/its @ 8/1.8.0 comes with + 312 / libexec/its JDK/Contents/Home/usr /local/ Cellar/its / 17.0.1 / libexec/its JDK/Contents/HomeCopy the code
Dynamically set Java Home
On the basis of dynamic search Java Home, combined with custom environment variables, alias, dynamic setting $JAVA_HOME(dynamic switch JDK version).
Find any of the configuration files such as.bash_profile/.bashrc/.zshrc and add the following code:
## JAVA
exportJAVA_HOME = $(/ usr/libexec/JAVA_HOME - v1.8)exportJAVA_8_HOME = $(/ usr/libexec/java_home - v1.8)export JAVA_11_HOME=$(/usr/libexec/java_home -v11)
export JAVA_17_HOME=$(/usr/libexec/java_home -v17)
## alias for JAVA
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java11='export JAVA_HOME=$JAVA_11_HOME'
alias java17='export JAVA_HOME=$JAVA_17_HOME'
Copy the code
Then exit terminal and open again, or reload configuration file:
# Take.zshrc for example
source ~/.zshrc
Copy the code
Use alias in the terminal, set the $JAVA_HOME environment variable, and view the version information:
➜ ~ java -version
openjdk version "1.8.0 comes with _312"OpenJDK Runtime Environment (build 1.8.0_312-BRE_2021_10_20_23_15-b00) Mixed mode) ➜ ~ java11 ➜ ~ Java -version OpenJDK version"11.0.12"2021-07-20 OpenJDK Runtime Environment Homebrew (build 11.0.12+0) Mixed mode) ➜ ~ java17 ➜ ~ Java -version OpenJDK version"17.0.1"2021-10-19 OpenJDK Runtime Environment Homebrew (build 17.0.1+0) mixed mode, sharing)Copy the code
How to improve the deficiencies
The $JAVA_HOME environment variable is set to alias, which can be used to switch JDK versions, but not persisted, just at the session level:
- Old window Settings when no version is saved
- No version is read when a new window opens
To make it persistent, you need to save the version information in a local file when setting the default version, and read the version information in the file for initialization when a new window opens.
Other JDK
Oracle JDK
The recently released Oracle JDK 17 can be used for free, and the Java SE Development Kit 17.0.1 Downloads
Since September 2021, Oracle provides the Oracle JDK for Java 17 and later under a free use license for All Users.
Beginning in September 2021, Oracle offers the Oracle JDK for Java 17 and later under a free use license for all users. Oracle Java SE Support Roadmap
The NFTC is the license for Oracle JDK 17 and later releases. Subject to the conditions of the license, Permitting free use for all users — even commercial and production use.
NFTC is a license for Oracle JDK 17 and later. Under the conditions of the license, it allows all users to use it free of charge — even for commercial and production purposes. — What is the new Oracle No-fee Terms and Conditions License (NFTC)?
Its other
- Adoptium OpenJDK
- MicroSoft OpenJDK
- Alibaba OpenJDK
- .
Pay attention to the point
JDK@8
Does not supportjava --version
, only one delimiter is supportedjava -version
Mac OS X 10.5
The above is supported/usr/libexec/java_home
Command line tool
conclusion
From JDK 1.5/1.6 in college to JDK 17 now, I can’t remember the number of times I looked online to find out how to install and where to download the latest version of the JDK.
I hesitate to write this article because there are many similar articles (how to install JDK, what is OpenJDK, what is Oracle JDK, etc.).
After thinking, I still feel that I should refine it again, trace back to the source, deepen my impression at the same time, but also record it for convenience.
Refer to the link
- How does the new Mac elegantly configure the Java development environment
- Java Developer Productivity Report 2021
- Oracle Releases Java 17
- Oracle Java SE Support Roadmap
- Important Java Directories on Mac OS X
- Oracle Java SE Licensing FAQ
- OpenJDK FAQ