Skip to content

Incorrect JAVA_HOME setting when selecting system JDK on macOS #244

@michelemadonna

Description

@michelemadonna

Description:

When selecting the system JDK on macOS using asdf-java, the JAVA_HOME environment variable is incorrectly set to /usr/bin/java. However, on macOS, the correct value for JAVA_HOME should be /usr/libexec/java_home.

This issue can be easily resolved by introducing a simple logic update in the set-java-home.* files to correctly handle the system JDK case.

Steps to Reproduce:

Install asdf-java and ensure it is correctly configured.

Set the system JDK using asdf set -u java system (for global) or asdf set java system for (local).

Check the JAVA_HOME environment variable (echo $JAVA_HOME).

Notice that it is set to /usr/bin/java instead of /usr/libexec/java_home.

Expected Behavior:

When selecting the system JDK on macOS, JAVA_HOME should be set to /usr/libexec/java_home to ensure proper Java environment configuration.

Proposed Solution:

Modify the set-java-home.* scripts to check for macOS and correctly set JAVA_HOME to /usr/libexec/java_home when the system JDK is selected.

asdf_update_java_home() {
  local java_path
  java_path="$(asdf which java)"
  if [[ -n "${java_path}" ]]; then
    export JAVA_HOME
    if [[ "$OSTYPE" == "darwin"* && "${java_path}" == "/usr/bin/java" ]]; then
    	JAVA_HOME="$(/usr/libexec/java_home)"
    else	
    	JAVA_HOME="$(dirname "$(dirname "${java_path:A}")")"
    fi
    export JDK_HOME=${JAVA_HOME}
  fi
}

autoload -U add-zsh-hook
add-zsh-hook precmd asdf_update_java_home

Environment:

OS: macOS 15.3.2
asd version 0.16.7
asdf-java version: latest
Java version: system (Oracle JDK 21 installed using brew)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions