Explain Codes LogoExplain Codes Logo

How do I install Java on Mac OSX allowing version switching?

java
version-management
java-installation
sdkman
Anton ShumikhinbyAnton Shumikhin·Nov 17, 2024
TLDR

Manage multiple Java versions on macOS by utilizing Homebrew for installation and jEnv for version control.

  1. Install Homebrew:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. Install Java (for example, OpenJDK 11):

    brew install openjdk@11
    
  3. Set up jEnv for efficient version handling:

    brew install jenv
    echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(jenv init -)"' >> ~/.zshrc
    source ~/.zshrc
    
  4. Link your Java version to jEnv and set it default:

    jenv add /usr/local/opt/openjdk@11
    jenv global 11
    

Quickly examine the Java version currently in use:

java -version

To switch versions smoothly:

jenv local 11

Alternative tools: SDKMAN! & asdf, Manual setup

While jEnv offers proficiency, consider utilizing SDKMAN! or asdf for enriched version management.

SDKMAN! is handy for multi Software Development Kits:

curl -s "https://get.sdkman.io" | bash    # Install SDKMAN!
source "$HOME/.sdkman/bin/sdkman-init.sh"  # Make SDKMAN! readily available
sdk install java 11.0.2.j9-adpt            # Install desired Java version using SDKMAN!

asdf can manage several runtime versions:

git clone https://github.com/asdf-vm/asdf.git ~/.asdf    # Clone and install asdf
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bash_profile     # Add asdf to bash_profile for persistence
asdf plugin-add java                                     # Add Java plugin to asdf
asdf install java adopt-openjdk-11.0.2+9                 # Install Java via asdf

For manual installation and thorough control, download OpenJDK binaries and explicitly set JAVA_HOME:

export JAVA_HOME=$(/usr/libexec/java_home -v 11)   # Set JAVA_HOME to desired version 
java -version                                      # Check if Java version has been updated

Smart management of previous JDKs

Here are some tidbits for managing existing Java installations without causing a catastrophe:

  • All Installed JDKs: Command /usr/libexec/java_home -V lists installed Java versions. So you don't accidentally forget one.
  • Default Version: Edit your .bash_profile to stomach the version of Java you're most comfortable with.
  • Version Hopping: Adjust your active Java version using the export JAVA_HOME=$(...) wizardry and reloading your profile. Pretty neat for party tricks.
  • Implicit JDK selection: By renaming Info.plist in specific JDK packages, you basically decide who's king of the hill.

Pro tips for power users

Quick version switches with aliases

If you switch between Java versions more often than you switch TV channels, generating aliases in your .bash_profile could be your remote control:

alias java8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)' # Channel "Java 8" alias java11='export JAVA_HOME=$(/usr/libexec/java_home -v 11)' # Channel "Java 11" alias java17='export JAVA_HOME=$(/usr/libexec/java_home -v 17)' # Channel "Java 17"

Customization and troubleshooting

  • Version-Specific Home: Like choosing your favorite couch, set JAVA_HOME in your bash profile to your preferred Java version.
  • Handle with Visibility: Control the visibility of each Java version by cunningly using the invisibility cloak, Info.plist files.
  • Fallback: If you forget to declare $JAVA_HOME, the system proves it's smarter than you and defaults to the most recent Java version. Tip the hat to your Mac.

Picking your Java flavor

Be it the universal OpenJDK or a vendor-specific version like AdoptOpenJDK, Amazon Corretto, or Oracle JDK, let project requirements and licensing guide your choice. Choose wisely — not all beans result in the same coffee.