Explain Codes LogoExplain Codes Logo

How to set or change the default Java (JDK) version on macOS?

java
prompt-engineering
functions
venv
Alex KataevbyAlex Kataev·Nov 12, 2024
TLDR

To swiftly configure your desired Java version, execute:

export JAVA_HOME=$(/usr/libexec/java_home -v [version_number])

Replace [version_number] with versions like 1.8, 11, etc. You can verify your switch with:

java -version

To have the version locked, add the export line to your ~/.bash_profile if you're a Bash user or ~/.zshrc for Zsh users.

Quick Session vs Permanent JDK Change

For an ephemeral session JDK switch, your terminal command suffices. But if you need your changes to persist, you should modify your shell's initialization script.

Bash users can use:

echo 'export JAVA_HOME=$(/usr/libexec/java_home -v [version_number])' >> ~/.bash_profile source ~/.bash_profile

And for Zsh users:

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

Validate the update by running echo $JAVA_HOME.

Streamlined Version Switching

Shift between JDK versions efficiently by employing aliases or functions in your shell profile.

Set an alias per version:

alias java11='export JAVA_HOME=$(/usr/libexec/java_home -v 11)' # Now you can __java11-and-chill__! 😎 alias java8='export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)' # Bringing the old-school back! 🕺

Or, a develop a function named jhome for dynamic JAVA_HOME setting:

jhome() { export JAVA_HOME=$(/usr/libexec/java_home -v $1) # Ah, `$1` takes the argument. Clever! 🕵️‍♂️ }

Copy-paste java11 or jhome 11 for switching.

Project-specific JDK Setting

Designate a distinct JDK for separate projects or sessions without affecting the system-wide default. Set the JAVA_HOME within your session or script related to that environment.

For a one-time session:

export JAVA_HOME=$(/usr/libexec/java_home -v 12) # YOLO JDK change for this session! 🚀

For an individual project, add the command to its startup script.

JDK Management Tools

Managing multiple JDK versions? jenv or SDKMAN! might be your saviors! These tools streamline the process of switching and maintaining Java environments.

Install JDKs with Homebrew:

brew install --cask temurin # Brew the latest version ☕ brew install --cask temurin8 # Brew some Java 8 nostalgia ☕

Manage JDKs using:

jenv add $(/usr/libexec/java_home -v [version_number]) # More JDKs? jEnv to the rescue! 💪

Or, with SDKMAN!:

sdk install java [version] # Example: sdk install java 11.0.3.hs-adpt -- SDKs? MAN! 🤓

Don't forget to untap outdated casks periodically:

brew untap homebrew/cask-versions # Cleaning outdated taps. Soup Singer's secret recipe 🍲

Troubleshooting Common Issues

Experiencing unresponsive commands or misidentification of versions? This should help:

  • If java_home omits versions, check Info.plist files under /Library/Java/JavaVirtualMachines.
  • JDK paths might change post system updates, recheck them.
  • Make sure multiple JAVA_HOME declarations aren't conflicting in your profile scripts.