|
#!/usr/bin/env bash |
|
|
|
################################################################################ |
|
# Setup SDKMAN JDK Integration with /usr/libexec/java_home |
|
################################################################################ |
|
# This script configures macOS to recognize SDKMAN-installed JDKs through |
|
# /usr/libexec/java_home by creating a proper directory structure and symlink |
|
# in /Library/Java/JavaVirtualMachines. |
|
# |
|
# Usage: |
|
# ./install.sh [install|uninstall|verify|help] [--force] [--quiet] |
|
# |
|
# Options: |
|
# install - Set up the SDKMAN JDK integration (default) |
|
# uninstall - Remove the SDKMAN JDK integration |
|
# verify - Check if the setup is working correctly |
|
# help - Display this help message |
|
# |
|
# --force - Reinstall even if the setup is already correct |
|
# --quiet - Only print warnings and errors (for unattended runs) |
|
# |
|
# Requirements: |
|
# - macOS |
|
# - SDKMAN installed with at least one JDK |
|
# - sudo privileges (see AUTHENTICATION below) |
|
# |
|
# AUTHENTICATION |
|
# Works unattended. Root is obtained from the first of these that succeeds: |
|
# 1. already running as root |
|
# 2. a cached sudo timestamp (sudo -n) |
|
# 3. SUDO_ASKPASS is set (sudo -A) |
|
# 4. a controlling terminal (sudo -v </dev/tty — supports Touch ID, |
|
# and works even when curl | bash) |
|
# Only a truly headless run with no cached credentials and no askpass helper |
|
# will fail, and it exits 3 so callers can detect that specific case. |
|
# |
|
# EXIT CODES |
|
# 0 success (including "already up to date") |
|
# 1 usage / unsupported platform |
|
# 2 SDKMAN or JDK missing |
|
# 3 could not obtain sudo non-interactively |
|
# 4 verification failed |
|
################################################################################ |
|
|
|
set -eo pipefail |
|
|
|
# Constants (temporarily disable -u for BASH_SOURCE check) |
|
readonly SCRIPT_NAME="$(basename "${BASH_SOURCE[0]:-install.sh}")" |
|
readonly SCRIPT_VERSION="2.0.0" |
|
|
|
# Re-enable unbound variable checking |
|
set -u |
|
readonly TARGET_DIR="/Library/Java/JavaVirtualMachines/sdkman-current" |
|
readonly CONTENTS_DIR="${TARGET_DIR}/Contents" |
|
readonly HOME_LINK="${CONTENTS_DIR}/Home" |
|
readonly PLIST_FILE="${CONTENTS_DIR}/Info.plist" |
|
|
|
# Runtime flags |
|
FORCE=false |
|
QUIET=false |
|
SUDO_READY=false |
|
|
|
# Colors for output (disabled when not a terminal, so logs stay readable) |
|
if [[ -t 1 ]]; then |
|
readonly RED='\033[0;31m' |
|
readonly GREEN='\033[0;32m' |
|
readonly YELLOW='\033[1;33m' |
|
readonly BLUE='\033[0;34m' |
|
readonly BOLD='\033[1m' |
|
readonly NC='\033[0m' |
|
else |
|
readonly RED='' GREEN='' YELLOW='' BLUE='' BOLD='' NC='' |
|
fi |
|
|
|
# Report where we died instead of exiting silently — an unattended run needs to |
|
# say why it failed, not just return non-zero. |
|
on_error() { |
|
local exit_code=$? line=${1:-?} |
|
if [[ $exit_code -ne 0 ]]; then |
|
echo -e "${RED}✗${NC} ${SCRIPT_NAME}: failed at line ${line} (exit ${exit_code})" >&2 |
|
fi |
|
exit $exit_code |
|
} |
|
|
|
trap 'on_error $LINENO' ERR |
|
|
|
################################################################################ |
|
# Utility Functions |
|
################################################################################ |
|
|
|
print_header() { |
|
[[ "$QUIET" == true ]] && return 0 |
|
echo -e "\n${BOLD}${BLUE}===${NC} ${BOLD}$1${NC} ${BOLD}${BLUE}===${NC}\n" |
|
} |
|
|
|
success() { |
|
[[ "$QUIET" == true ]] && return 0 |
|
echo -e "${GREEN}✓${NC} $1" |
|
} |
|
|
|
info() { |
|
[[ "$QUIET" == true ]] && return 0 |
|
echo -e "${BLUE}ℹ${NC} $1" |
|
} |
|
|
|
warn() { |
|
echo -e "${YELLOW}⚠${NC} $1" >&2 |
|
} |
|
|
|
error() { |
|
echo -e "${RED}✗${NC} $1" >&2 |
|
} |
|
|
|
# Check if running on macOS |
|
check_macos() { |
|
if [[ "$(uname -s)" != "Darwin" ]]; then |
|
error "This script only works on macOS" |
|
exit 1 |
|
fi |
|
} |
|
|
|
# Check if SDKMAN is installed |
|
check_sdkman() { |
|
local sdkman_dir="${SDKMAN_DIR:-$HOME/.sdkman}" |
|
|
|
if [[ ! -d "$sdkman_dir" ]]; then |
|
error "SDKMAN not found at $sdkman_dir" |
|
info "Install SDKMAN from: https://sdkman.io/install" |
|
exit 2 |
|
fi |
|
|
|
success "SDKMAN found at $sdkman_dir" |
|
} |
|
|
|
# Get SDKMAN current JDK path |
|
get_sdkman_current_jdk() { |
|
local sdkman_dir="${SDKMAN_DIR:-$HOME/.sdkman}" |
|
local current_jdk="${sdkman_dir}/candidates/java/current" |
|
|
|
if [[ ! -e "$current_jdk" ]]; then |
|
error "No current JDK set in SDKMAN" |
|
info "Install a JDK with: sdk install java" |
|
info "Or set current with: sdk use java <version>" |
|
exit 2 |
|
fi |
|
|
|
# Deliberately return the 'current' symlink, not its target: pointing at it |
|
# is what makes `sdk default java <ver>` retarget this integration for free. |
|
echo "$current_jdk" |
|
} |
|
|
|
# Get JDK version — prefer the SDKMAN candidate name (e.g. 21.0.12-tem), which |
|
# is accurate for every distribution and needs no shell sourcing. |
|
get_jdk_version() { |
|
local jdk_path="$1" |
|
local version="" |
|
|
|
if [[ -L "$jdk_path" ]]; then |
|
version="$(basename "$(readlink "$jdk_path")")" |
|
fi |
|
|
|
if [[ -z "$version" ]]; then |
|
local release="${jdk_path}/release" |
|
[[ -f "$release" ]] && version="$(sed -n 's/^JAVA_VERSION="\(.*\)"/\1/p' "$release" | head -1)" |
|
fi |
|
|
|
echo "${version:-unknown}" |
|
} |
|
|
|
# Get JDK vendor from the candidate name (e.g. 21.0.12-tem -> Eclipse Temurin) |
|
get_jdk_vendor() { |
|
local jdk_path="$1" |
|
local vendor="SDKMAN" |
|
local name="" |
|
|
|
if [[ -L "$jdk_path" ]]; then |
|
name="$(basename "$(readlink "$jdk_path")")" |
|
else |
|
name="$(basename "$jdk_path")" |
|
fi |
|
|
|
if [[ "$name" =~ -([a-z]+)$ ]]; then |
|
local vendor_code="${BASH_REMATCH[1]}" |
|
case "$vendor_code" in |
|
amzn) vendor="Amazon Corretto" ;; |
|
tem) vendor="Eclipse Temurin" ;; |
|
zulu) vendor="Azul Zulu" ;; |
|
graal*) vendor="GraalVM" ;; |
|
librca|liberica) vendor="BellSoft Liberica" ;; |
|
sapmchn) vendor="SAP Machine" ;; |
|
ms) vendor="Microsoft OpenJDK" ;; |
|
oracle) vendor="Oracle JDK" ;; |
|
open) vendor="OpenJDK" ;; |
|
sem) vendor="IBM Semeru" ;; |
|
*) vendor="SDKMAN ($vendor_code)" ;; |
|
esac |
|
fi |
|
|
|
echo "$vendor" |
|
} |
|
|
|
# Anything at the target path counts as existing — including a plain file or a |
|
# broken symlink, which -d alone would miss and which would then make mkdir fail. |
|
check_existing_setup() { |
|
[[ -e "$TARGET_DIR" || -L "$TARGET_DIR" ]] |
|
} |
|
|
|
# True only when the integration is already complete and pointing at the current |
|
# SDKMAN JDK, so repeat runs (bootstrap scripts, CI, cron) can no-op safely. |
|
is_setup_current() { |
|
local expected="$1" |
|
|
|
[[ -L "$HOME_LINK" ]] || return 1 |
|
[[ "$(readlink "$HOME_LINK")" == "$expected" ]] || return 1 |
|
[[ -e "$HOME_LINK" ]] || return 1 |
|
[[ -f "$PLIST_FILE" ]] || return 1 |
|
[[ "$(/usr/libexec/java_home 2>/dev/null)" == "$HOME_LINK" ]] || return 1 |
|
|
|
return 0 |
|
} |
|
|
|
# Obtain root without ever blocking on a prompt nobody can answer. |
|
verify_sudo() { |
|
[[ "$SUDO_READY" == true ]] && return 0 |
|
|
|
# 1. Already root (e.g. re-exec'd, or run from a root context) |
|
if [[ "$(id -u)" -eq 0 ]]; then |
|
SUDO_READY=true |
|
return 0 |
|
fi |
|
|
|
# 2. Cached sudo timestamp |
|
if sudo -n true 2>/dev/null; then |
|
SUDO_READY=true |
|
keep_sudo_alive |
|
return 0 |
|
fi |
|
|
|
# 3. Askpass helper (headless-friendly: CI, MDM, provisioning) |
|
if [[ -n "${SUDO_ASKPASS:-}" ]] && sudo -A -v 2>/dev/null; then |
|
SUDO_READY=true |
|
keep_sudo_alive |
|
return 0 |
|
fi |
|
|
|
# 4. A controlling terminal. Reading from /dev/tty rather than stdin is what |
|
# makes this work under `curl ... | bash` — and lets Touch ID answer it. |
|
# -r /dev/tty is not enough: the device node is readable even when the |
|
# process has no controlling terminal, so actually open it to find out. |
|
if { exec 3< /dev/tty; } 2>/dev/null; then |
|
exec 3<&- |
|
if sudo -v < /dev/tty; then |
|
SUDO_READY=true |
|
keep_sudo_alive |
|
return 0 |
|
fi |
|
fi |
|
|
|
error "Could not obtain sudo privileges non-interactively" |
|
echo "" >&2 |
|
echo "Provide credentials in one of these ways:" >&2 |
|
echo "" >&2 |
|
echo " # cache them first, then re-run" >&2 |
|
echo " sudo -v && curl -fsSL <gist-raw-url> | bash -s install" >&2 |
|
echo "" >&2 |
|
echo " # or supply an askpass helper (headless / CI)" >&2 |
|
echo " SUDO_ASKPASS=/path/to/askpass curl -fsSL <gist-raw-url> | bash -s install" >&2 |
|
exit 3 |
|
} |
|
|
|
# Refresh the timestamp while we work; dies with the parent. |
|
keep_sudo_alive() { |
|
[[ "$(id -u)" -eq 0 ]] && return 0 |
|
( while true; do sudo -n true 2>/dev/null || exit; sleep 60; kill -0 "$$" 2>/dev/null || exit; done ) & |
|
} |
|
|
|
# Run a privileged command (no-op wrapper when already root) |
|
as_root() { |
|
if [[ "$(id -u)" -eq 0 ]]; then |
|
"$@" |
|
else |
|
sudo "$@" |
|
fi |
|
} |
|
|
|
################################################################################ |
|
# Installation Functions |
|
################################################################################ |
|
|
|
install_setup() { |
|
print_header "Installing SDKMAN JDK Integration" |
|
|
|
local sdkman_jdk jdk_version jdk_vendor |
|
sdkman_jdk="$(get_sdkman_current_jdk)" |
|
jdk_version="$(get_jdk_version "$sdkman_jdk")" |
|
jdk_vendor="$(get_jdk_vendor "$sdkman_jdk")" |
|
|
|
info "SDKMAN Current JDK: $sdkman_jdk" |
|
info "Version: $jdk_version" |
|
info "Vendor: $jdk_vendor" |
|
|
|
# Idempotent: a correct setup is left completely alone unless --force. |
|
if [[ "$FORCE" != true ]] && is_setup_current "$sdkman_jdk"; then |
|
success "Integration already installed and current — nothing to do" |
|
info "Use --force to reinstall" |
|
return 0 |
|
fi |
|
|
|
verify_sudo |
|
|
|
# Remove whatever is there first. TARGET_DIR may be a symlink (an earlier |
|
# hand-rolled fix), a stale directory, or a broken link; in every case |
|
# mkdir -p would either fail or, worse, follow the symlink and create |
|
# Contents/ *inside the JDK itself*. |
|
if check_existing_setup; then |
|
info "Removing existing $TARGET_DIR..." |
|
as_root rm -rf "$TARGET_DIR" |
|
fi |
|
|
|
info "Creating directory structure..." |
|
as_root mkdir -p "$CONTENTS_DIR" |
|
success "Created $CONTENTS_DIR" |
|
|
|
info "Creating symlink to SDKMAN JDK..." |
|
as_root ln -sfn "$sdkman_jdk" "$HOME_LINK" |
|
success "Created symlink: $HOME_LINK -> $sdkman_jdk" |
|
|
|
info "Creating Info.plist..." |
|
as_root tee "$PLIST_FILE" > /dev/null << EOF |
|
<?xml version="1.0" encoding="UTF-8"?> |
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
|
<plist version="1.0"> |
|
<dict> |
|
<key>CFBundleIdentifier</key> |
|
<string>sdkman.current</string> |
|
<key>CFBundleName</key> |
|
<string>SDKMAN Current JDK</string> |
|
<key>CFBundleVersion</key> |
|
<string>${jdk_version}</string> |
|
<key>JavaVM</key> |
|
<dict> |
|
<key>JVMPlatformVersion</key> |
|
<string>9999</string> |
|
<key>JVMVendor</key> |
|
<string>${jdk_vendor}</string> |
|
<key>JVMVersion</key> |
|
<string>9999</string> |
|
</dict> |
|
</dict> |
|
</plist> |
|
EOF |
|
success "Created Info.plist" |
|
|
|
info "Setting permissions..." |
|
as_root chmod -R 755 "$TARGET_DIR" |
|
success "Permissions set" |
|
|
|
print_header "Installation Complete" |
|
success "SDKMAN JDK integration installed successfully" |
|
|
|
verify_setup |
|
} |
|
|
|
################################################################################ |
|
# Uninstallation Functions |
|
################################################################################ |
|
|
|
uninstall_setup() { |
|
print_header "Uninstalling SDKMAN JDK Integration" |
|
|
|
if ! check_existing_setup; then |
|
warn "No existing setup found at $TARGET_DIR" |
|
info "Nothing to uninstall" |
|
return 0 |
|
fi |
|
|
|
verify_sudo |
|
|
|
info "Removing $TARGET_DIR..." |
|
as_root rm -rf "$TARGET_DIR" |
|
success "Removed SDKMAN JDK integration" |
|
|
|
print_header "Uninstallation Complete" |
|
} |
|
|
|
################################################################################ |
|
# Verification Functions |
|
################################################################################ |
|
|
|
verify_setup() { |
|
print_header "Verifying Setup" |
|
|
|
if ! check_existing_setup; then |
|
error "Setup not found at $TARGET_DIR" |
|
info "Run with 'install' to set up the integration" |
|
exit 4 |
|
fi |
|
|
|
if [[ ! -L "$HOME_LINK" ]]; then |
|
error "Symlink not found at $HOME_LINK" |
|
exit 4 |
|
fi |
|
success "Symlink exists: $HOME_LINK" |
|
|
|
if [[ ! -e "$HOME_LINK" ]]; then |
|
error "Broken symlink: $HOME_LINK -> $(readlink "$HOME_LINK")" |
|
info "The JDK it pointed at was probably removed — re-run: $SCRIPT_NAME install" |
|
exit 4 |
|
fi |
|
success "Symlink target valid: $(readlink "$HOME_LINK")" |
|
|
|
if [[ ! -f "$PLIST_FILE" ]]; then |
|
error "Info.plist not found at $PLIST_FILE" |
|
exit 4 |
|
fi |
|
success "Info.plist exists" |
|
|
|
# A malformed plist makes java_home skip the JDK without any obvious reason. |
|
if command -v plutil >/dev/null 2>&1; then |
|
if plutil -lint "$PLIST_FILE" >/dev/null 2>&1; then |
|
success "Info.plist is valid" |
|
else |
|
error "Info.plist is malformed — java_home will ignore this JDK" |
|
exit 4 |
|
fi |
|
fi |
|
|
|
if [[ ! -x "$HOME_LINK/bin/java" ]]; then |
|
error "No java binary at $HOME_LINK/bin/java" |
|
exit 4 |
|
fi |
|
success "java binary present: $("$HOME_LINK/bin/java" -version 2>&1 | head -1)" |
|
|
|
info "Testing /usr/libexec/java_home..." |
|
local java_home_output |
|
if ! java_home_output=$(/usr/libexec/java_home 2>&1); then |
|
error "/usr/libexec/java_home failed" |
|
echo "$java_home_output" >&2 |
|
exit 4 |
|
fi |
|
|
|
# Resolving is not enough — it must resolve to *this* integration. |
|
if [[ "$java_home_output" != "$HOME_LINK" ]]; then |
|
error "/usr/libexec/java_home resolves elsewhere: $java_home_output" |
|
info "Another JDK is winning. Re-run with --force, or remove that JDK." |
|
exit 4 |
|
fi |
|
success "/usr/libexec/java_home -> $java_home_output" |
|
|
|
if [[ "$QUIET" != true ]]; then |
|
echo -e "\n${BOLD}Available Java Virtual Machines:${NC}" |
|
/usr/libexec/java_home -V 2>&1 || true |
|
fi |
|
|
|
print_header "Verification Complete" |
|
success "Setup is working correctly" |
|
|
|
if [[ "$QUIET" != true ]]; then |
|
echo |
|
info "Notes:" |
|
echo " • When you change JDK with 'sdk use java <version>', the symlink" |
|
echo " will automatically point to the new current version" |
|
echo " • Xcode and other tools can now find Java using /usr/libexec/java_home" |
|
echo " • The version is set to 9999 to ensure this JDK is always selected" |
|
fi |
|
} |
|
|
|
################################################################################ |
|
# Help Function |
|
################################################################################ |
|
|
|
show_help() { |
|
cat << 'EOF' |
|
install.sh - Version 2.0.0 |
|
|
|
DESCRIPTION |
|
Configure macOS to recognize SDKMAN-installed JDKs through /usr/libexec/java_home |
|
by creating a proper directory structure in /Library/Java/JavaVirtualMachines. |
|
|
|
USAGE |
|
install.sh [COMMAND] [OPTIONS] |
|
|
|
COMMANDS |
|
install Set up the SDKMAN JDK integration (default) |
|
uninstall Remove the SDKMAN JDK integration |
|
verify Verify the setup is working correctly |
|
help Display this help message |
|
|
|
OPTIONS |
|
--force Reinstall even when the setup is already correct |
|
--quiet Suppress informational output (warnings/errors still shown) |
|
|
|
EXAMPLES |
|
# Install the integration |
|
install.sh install |
|
|
|
# Verify it's working |
|
install.sh verify |
|
|
|
# Remove the integration |
|
install.sh uninstall |
|
|
|
# Unattended (bootstrap scripts, CI): no-ops when already correct |
|
sudo -v && curl -fsSL <gist-raw-url> | bash -s install --quiet |
|
|
|
AUTHENTICATION |
|
Root is obtained from the first of these that works: already-root, a cached |
|
sudo timestamp, $SUDO_ASKPASS, or a controlling terminal (/dev/tty — which |
|
keeps Touch ID working even under `curl | bash`). Exits 3 if none apply. |
|
|
|
IDEMPOTENCY |
|
'install' checks the existing setup first and exits 0 without touching |
|
anything when it is already correct, so it is safe to run repeatedly from |
|
provisioning scripts. Use --force to rebuild regardless. |
|
|
|
HOW IT WORKS |
|
1. Creates /Library/Java/JavaVirtualMachines/sdkman-current/Contents/ |
|
2. Symlinks Contents/Home to SDKMAN's current JDK |
|
3. Creates Info.plist with proper metadata |
|
4. Sets version to 9999 to ensure it's always selected |
|
|
|
REQUIREMENTS |
|
- macOS |
|
- SDKMAN installed with at least one JDK |
|
- sudo privileges |
|
|
|
EXIT CODES |
|
0 success (including "already up to date") |
|
1 usage error / not macOS |
|
2 SDKMAN or JDK missing |
|
3 could not obtain sudo non-interactively |
|
4 verification failed |
|
|
|
FILES CREATED |
|
/Library/Java/JavaVirtualMachines/sdkman-current/ |
|
/Library/Java/JavaVirtualMachines/sdkman-current/Contents/ |
|
/Library/Java/JavaVirtualMachines/sdkman-current/Contents/Home (symlink) |
|
/Library/Java/JavaVirtualMachines/sdkman-current/Contents/Info.plist |
|
|
|
MORE INFO |
|
- SDKMAN: https://sdkman.io/ |
|
- Gist: https://gist.github.com/abd3lraouf/1db9bf863144802733bfd29bb5dada87 |
|
|
|
EOF |
|
} |
|
|
|
################################################################################ |
|
# Main Function |
|
################################################################################ |
|
|
|
main() { |
|
check_macos |
|
|
|
local command="" |
|
|
|
# Flags may appear before or after the command. |
|
for arg in "$@"; do |
|
case "$arg" in |
|
--force|-f) FORCE=true ;; |
|
--quiet|-q) QUIET=true ;; |
|
--version|-V) echo "$SCRIPT_NAME $SCRIPT_VERSION"; exit 0 ;; |
|
-*) error "Unknown option: $arg"; echo >&2; show_help >&2; exit 1 ;; |
|
*) [[ -z "$command" ]] && command="$arg" ;; |
|
esac |
|
done |
|
|
|
case "${command:-install}" in |
|
install) |
|
check_sdkman |
|
install_setup |
|
;; |
|
uninstall) |
|
uninstall_setup |
|
;; |
|
verify) |
|
check_sdkman |
|
verify_setup |
|
;; |
|
help) |
|
show_help |
|
;; |
|
*) |
|
error "Unknown command: $command" |
|
echo |
|
show_help |
|
exit 1 |
|
;; |
|
esac |
|
} |
|
|
|
main "$@" |