W3docs

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:

  1. Open a Terminal window.

  2. Use the /usr/libexec/java_home command to list the available JDK versions:

    /usr/libexec/java_home -V

    This will list the available JDK versions and their corresponding paths.

  3. To set the default JDK version permanently, add an export statement to your shell configuration file. macOS Catalina and later use zsh by default. Run the following command, replacing 11 with your desired major version:

    echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 11)' >> ~/.zshrc
    source ~/.zshrc

    (For older macOS versions using bash, replace ~/.zshrc with ~/.bash_profile)

  4. To verify that the default JDK version has been set correctly, use the java -version command:

    java -version

    This 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.