How Can The Default Java (jdk) Version Be Set Or Modified On Macos Using Java?

To set or change the default Java Development Kit (JDK) version on macOS, you can follow these steps:

  1. Open Terminal. You can find it in the Applications > Utilities folder.

  2. Check the current JDK versions installed on your system by running the command: java -version.

  3. If you have multiple JDK versions installed, you can list them using the ls /Library/Java/JavaVirtualMachines command.

  4. Set the desired JDK version as default by running the following command:

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home

    Replace <version> with the desired JDK version number, like 1.8.0_301 or 11.0.12.

  5. Verify that the JAVA_HOME environment variable is set correctly by running echo $JAVA_HOME. It should display the path to the JDK version you set.

  6. To make this change permanent, add the export command from step 4 to your shell profile file, such as ~/.bash_profile or ~/.zshrc. Open the file in a text editor and add the following line at the end:

    export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home

    Save the file and close the text editor.

  7. Restart Terminal or run source ~/.bash_profile (or source ~/.zshrc) to apply the changes to the current shell session.

Now, the specified JDK version should be set as the default for your macOS system.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!