How to set or change the default Java (JDK) version on macOS?
To set the default Java (JDK) version on macOS, you can use the java_home command line tool. Here's how:
To set the default Java (JDK) version on macOS, you can use the /usr/libexec/java_home command line tool to locate the installation path, then configure your shell to use it permanently. Here's how:
-
Open a Terminal window.
-
Use the
/usr/libexec/java_homecommand to list the available JDK versions:/usr/libexec/java_home -VThis will list the available JDK versions and their corresponding paths.
-
To set the default JDK version permanently, add an export statement to your shell configuration file. macOS Catalina and later use
zshby default. Run the following command, replacing11with your desired major version:echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 11)' >> ~/.zshrc source ~/.zshrc(For older macOS versions using
bash, replace~/.zshrcwith~/.bash_profile) -
To verify that the default JDK version has been set correctly, use the
java -versioncommand:java -versionThis will print the version of the JDK that is currently being used.
Note that these steps will only set the default JDK version for the current user. If you want to set the default JDK version for all users on the system, you will need to use additional steps.