Xem 18,513
Cập nhật thông tin chi tiết về How To Set $Java_Home Environment Variable On Macos mới nhất ngày 29/05/2022 trên website Kichcauhocvan.net. Hy vọng nội dung bài viết sẽ đáp ứng được nhu cầu của bạn, chúng tôi sẽ thường xuyên cập nhật mới nội dung để bạn nhận được thông tin nhanh chóng và chính xác nhất. Cho đến thời điểm hiện tại, bài viết này đã đạt được 18,513 lượt xem.
--- Bài mới hơn ---
This article shows how to set the $JAVA_HOME
environment variable on older Mac OS X and the latest macOS 11.
Topics
- macOS release history
- What is /usr/libexec/java_home
- $JAVA_HOME and macOS 11 Big Sur
- $JAVA_HOME and Mac OS X 10.5 Leopard
- $JAVA_HOME and older Mac OS X
- Switch between different JDK versions
Solution
Steps to set the $JAVA_HOME
environment variable on macOS.
- Find out your macOS version.
- Find out which shell you are using, bash or zsh?
- For zsh shell, export
$JAVA_HOME
at~/.zshenv
or~/.zshrc
. - For bash shell, export
$JAVA_HOME
at~/.bash_profile
or~/.bashrc
. - Test with
echo $JAVA_HOME
. - Done.
1. macOS release history, bash or zsh?
1.1 Review the macOS release history, source Wikipedia – macOS.
- Mac OS X Public Beta
- Mac OS X 10.0 (Cheetah)
- Mac OS X 10.1 (Puma)
- Mac OS X 10.2 Jaguar
- Mac OS X 10.3 Panther
- Mac OS X 10.4 Tiger
- Mac OS X 10.5 Leopard
- Mac OS X 10.6 Snow Leopard
- Mac OS X 10.7 Lion
- OS X 10.8 Mountain Lion
- OS X 10.9 Mavericks
- OS X 10.10 Yosemite
- OS X 10.11 El Capitan
- macOS 10.12 Sierra
- macOS 10.13 High Sierra
- macOS 10.14 Mojave
- macOS 10.15 Catalina (zsh)
- macOS 11 Big Sur (zsh)
1.2 bash or zsh?
On macOS 10.15 Catalina and later, the default Terminal shell switch from the bash (Bourne-again shell) to zsh (Z shell).
- For bash shell, we can put the environment variables at
~/.bash_profile
or~/.bashrc
. - For zsh shell, we can put the environment variables at
~/.zshenv
or~/.zshrc
.
We can print the $SHELL
environment variable to determine the current shell you are using.
% echo $SHELL /bin/zsh
Further Reading
2. What is /usr/libexec/java_home
2.1 On Mac OS X 10.5 or later, we can use /usr/libexec/java_home
to return the location of the default JDK.
% /usr/libexec/java_home chúng tôi
2.2 Also, find all installed JDKs.
% /usr/libexec/java_home -V Matching Java Virtual Machines (4): 16 (x86_64) "Oracle Corporation" - "OpenJDK 16-ea" chúng tôi 15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" chúng tôi 14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" chúng tôi 1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" chúng tôi chúng tôi
2.3 Also, run a specified JDK command.
% /usr/libexec/java_home -v1.8 chúng tôi
3. $JAVA_HOME and macOS 11 Big Sur
On macOS 10.15 Catalina and later, the zsh
is the default Terminal shell, and we can set the $JAVA_HOME
environment variable in either ~/.zshenv
or ~/.zshrc
.
3.1 Open the ~/.zshenv
% nano ~/.zshenv
3.2 Add the following content
export JAVA_HOME=$(/usr/libexec/java_home)
3.3 Source the file and print the $JAVA_HOME
, done.
% source ~/.zshenv % echo $JAVA_HOME chúng tôi
4. $JAVA_HOME and Mac OS X 10.5 Leopard
For older Mac OS X, the bash
is the default Terminal shell, and we can set the $JAVA_HOME
environment variable in either ~/.bash_profile
or ~/.bashrc
.
4.1 Open the ~/.bash_profile
% nano ~/.bash_profile
4.2 Add the following content
export JAVA_HOME=$(/usr/libexec/java_home)
4.3 Source the file and print the $JAVA_HOME
% source ~/.bash_profile % echo $JAVA_HOME chúng tôi
5. $JAVA_HOME and older Mac OS X
On older Mac OS X, the tool /usr/libexec/java_home
doesn’t exists, and we need to set the $JAVA_HOME
to the real path.
5.1 Open the ~/.bash_profile
% nano ~/.bash_profile
5.2 Add the following content
export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
5.3 Source the file and print the $JAVA_HOME
% source ~/.bash_profile % echo $JAVA_HOME chúng tôi
6. Switch between different JDK versions
For example, this macOS contains four JDK: 1.8, 14, 15, and 16, and the default JDK is 16.
% /usr/libexec/java_home -V Matching Java Virtual Machines (4): 16 (x86_64) "Oracle Corporation" - "OpenJDK 16-ea" chúng tôi 15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" chúng tôi 14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" chúng tôi 1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" chúng tôi chúng tôi
6.1 For zsh
shell, edit the ~/.zshenv
% nano ~/.zshenv
6.2 /usr/libexec/java_home -v"{$Version}"
to activate a specified JDK version.
Add the following content to activate the JDK 1.8
export JAVA_HOME=$(/usr/libexec/java_home -v1.8)
If we want JDK 14.
export JAVA_HOME=$(/usr/libexec/java_home -v14)
If we want JDK 15.
export JAVA_HOME=$(/usr/libexec/java_home -v15)
6.3 Source the file and print the $JAVA_HOME
, done.
% source ~/.zshenv % echo $JAVA_HOME chúng tôi
References
--- Bài cũ hơn ---
Bạn đang xem bài viết How To Set $Java_Home Environment Variable On Macos trên website Kichcauhocvan.net. Hy vọng những thông tin mà chúng tôi đã chia sẻ là hữu ích với bạn. Nếu nội dung hay, ý nghĩa bạn hãy chia sẻ với bạn bè của mình và luôn theo dõi, ủng hộ chúng tôi để cập nhật những thông tin mới nhất. Chúc bạn một ngày tốt lành!