Explain Codes LogoExplain Codes Logo

Mac OS X and multiple Java versions

java
homebrew
java-versions
sdkman
Nikita BarsukovbyNikita Barsukov·Jan 8, 2025
TLDR

Seamlessly manage multiple Java versions on macOS using jEnv. Here's your quick start guide:

Install jEnv:

brew install jenv

Add your desired Java version:

jenv add $(/usr/libexec/java_home -v<version>) # replace <version> with your version number

Change version globally:

jenv global <version> # Better control with a global setting

Or alter for a single project:

jenv local <version> # Perfect for project-specific requirements

Set up your Java version for a session:

export JAVA_HOME=$(/usr/libexec/java_home -v<version>) # Setting the mood for a Java session

Swap <version> with your desired Java version number, because who doesn't want power at their fingertips?

Harnessing Homebrew

Homebrew is a package manager for macOS — think of it as a personal software assistant. With homebrew and homebrew-cask, managing multiple Java installations becomes a breeze.

Tap into the treasure trove of Java versions with homebrew/cask-versions.

brew tap homebrew/cask-versions #◝(•‿•)◜ brew cask install java<version> # Suddenly, any version's within reach

Optimizing .bashrc or .zshrc

Handy scripts that automate your Java environment setup? Count me in! These go into .bashrc or .zshrc.

For jEnv:

eval "$(jenv init -)" # Add this to your file, sit back and watch jEnv do its magic

And for SDKMAN!:

export SDKMAN_DIR="$HOME/.sdkman" [[ -s "$SDKMAN_DIR/bin/sdkman-init.sh" ]] && source "$SDKMAN_DIR/bin/sdkman-init.sh" # Wave the magic wand and SDKMAN! springs into action

Changing the game with SDKMAN!

SDKMAN! stands tall as an excellent alternative for managing your collection of Java versions.

Install SDKMAN! with bear-like simplicity:

curl -s "https://get.sdkman.io" | bash # Like summoning a spell‍

List all available versions:

sdk list java # It's a long list, don't get lost!

Install the specific version of Java you latest fancy:

sdk install java <version> # "Only I can change my life..."

Switching between versions? SDKMAN! made that uncomplicated:

sdk use java <version> # "...No one can do it for me", huh? Challenge accepted!

Visualizing your Java environment

Managing different Java versions on macOS is akin to maneuvering a toolbox (🧰). Each tool is a different version of Java.

🧰 macOS Java Toolbox:

Box contents:

  • Adjustable wrench (🔧): Your System Java, always at your service!
  • Screwdriver set (🪛): Installed Java Versions, waiting for the summon.

Picking the right tool (Java version):

🛠️ Selecting Java Version:

  1. 🔧 System Java - Ready at default
  2. 🪛 Specific Java - Your ace in the hole

Switching tools (Java version):

🔄 Switch: 🪛 Use '/usr/libexec/java_home -v'

Behold the transformation:

Before: [🔧🔧🔧🔧] After Switch: [🪛🪛🔧🔧] // The first two tasks now use the specific Java version!

Becoming a Java version maestro

Aliases: Your secret weapon

Setting up aliases for swift version switching? Revolutionary, I know.

alias java8='export JAVA_HOME=$(/usr/libexec/java_home -v1.8)' # Typing's too mainstream, let's use aliases! alias java11='export JAVA_HOME=$(/usr/libexec/java_home -v11)' # Get ready for type-less magic

When things get murky...

Trouble brewing? Here's your contingency plan:

  • Review the installation with /usr/libexec/java_home -V.
  • Verify your JAVA_HOME exportation; the app you are launching might be looking for it.
  • Check the effective Java version with java -version.

Setting a lasting version

Because it's the 21st century and automation rocks, set your preferred version to load automatically in .bashrc or .zshrc:

jenv global <version>

Lean back and relax, your automatic Java version switch is happening!