🤓 How to Change Your Default JDK on macOS: A Step-by-Step Guide with a Splash of Humor!

Hey there, fellow Java enthusiasts and macOS aficionados! 👋 If you're reading this, chances are you've got a hankering to switch up your Java Development Kit (JDK) version on your trusty Mac. Fear not, for I'm here to guide you through the process with a dash of wit and a sprinkle of charm. 😄 Let's dive into the nitty-gritty of setting or changing your default JDK version on macOS!

1. Check Your Current JDK Version 🕵️‍♂️

Before we start changing things up, let's see what we're working with. Open up your Terminal and type:

java -version

This will show you the current version of Java that's being used. If you're like me and have multiple versions of Java installed, you might see something like this:

java version "1.8.0_292"
Java(TM) SE Runtime Environment (build 1.8.0_292-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.292-b10, mixed mode)

2. Install Homebrew if You Haven't Already 🍺

Homebrew is a package manager for macOS that makes installing software a breeze. If you haven't installed it yet, here's how:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Run that in your Terminal, and you'll be on your way to Homebrew bliss!

3. Install or Update JDKs with Homebrew 🛠️

Homebrew makes it super easy to install or update JDKs. First, tap the homebrew/cask-versions repository:

brew tap homebrew/cask-versions

Now, you can install a specific version of the JDK. For example, to install JDK 11, you'd run:

brew install --cask java11

If you want to install JDK 8, just replace java11 with java8.

4. List Installed JDKs 📋

Once you've got your JDKs installed, you can list them all to see what's available:

/usr/libexec/java_home -V

This command will show you a list of all installed JDKs, and you can pick the one you want to set as default.

5. Set the Default JDK 🏆

Now, for the main event! To set your default JDK, you'll use the export command in your .zshrc or .bash_profile, depending on which shell you're using. Open up your profile file with a text editor:

open -e ~/.zshrc

Or, if you're using Bash:

open -e ~/.bash_profile

Add the following line to the end of the file, replacing /path/to/java_home with the path to the JDK you want to set as default:

export JAVA_HOME=$(/usr/libexec/java_home -v 11) # Replace '11' with your desired version

Save the file and close the editor. Then, run:

source ~/.zshrc

Or, if you're using Bash:

source ~/.bash_profile

6. Verify Your New Default JDK 🔍

To make sure everything went smoothly, check your Java version again:

java -version

If all is well, you should see your new default JDK version displayed.

7. Celebrate Your Success 🎉

Congratulations! You've successfully changed your default JDK on macOS. Now you can go forth and code with the Java version of your choice. 🌟

And there you have it, folks! Changing your JDK version on macOS is a piece of cake, especially with the help of Homebrew. Remember, always keep your tools updated and enjoy the coding journey! 🛠️💻

Happy coding, and may your compile times be short and your code bug-free! 🐞👋