Home >Java >javaTutorial >How Do I Set a Default Java Version on macOS?

How Do I Set a Default Java Version on macOS?

Susan Sarandon
Susan SarandonOriginal
2024-12-14 11:19:11966browse

How Do I Set a Default Java Version on macOS?

How to Set the Default Java Version on macOS

Changing the default Java version on a Mac requires modifying environment variables. Here are the steps:

Run the following command to view available Java versions:

/usr/libexec/java_home -V

Select the desired Java version (e.g., 1.8.0_05) and export the JAVA_HOME environment variable using:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_05`

Alternatively, specify only the major version:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Verify the default Java version:

java -version

Add the export command to your shell's initialization file. For example, in Bash:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

For Fish:

set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)

For Zsh (in .zshrc file):

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)

Save the changes and source the file:

source ~/.zshrc

Confirm the updated default Java version:

echo $JAVA_HOME
java -version

The above is the detailed content of How Do I Set a Default Java Version on macOS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn