Skip to content

Instantly share code, notes, and snippets.

@MacsInSpace
Created January 15, 2025 11:30
Show Gist options
  • Save MacsInSpace/a0941d4c876b01b05584c02599098416 to your computer and use it in GitHub Desktop.
Save MacsInSpace/a0941d4c876b01b05584c02599098416 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Initialize jenv
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
# Extract all unique `/Contents/Home` paths
java_home_paths=($( /usr/libexec/java_home -V 2>&1 | grep '/Contents/Home' | awk '{for(i=1;i<=NF;i++) if($i ~ "/Contents/Home") print $i}' | sort -u ))
# Check if any paths were found
if [ ${#java_home_paths[@]} -eq 0 ]; then
echo "No Java installations found using /usr/libexec/java_home -V."
exit 1
fi
# Print the detected Java home paths
echo "Detected unique Java home paths:"
for path in "${java_home_paths[@]}"; do
echo "$path"
# Attempt to remove the Java home directory from jenv
if jenv remove "$path" 2>/dev/null; then
echo "Script Removed $path from jenv."
else
echo "Failed to remove $path (not found in jenv)."
fi
# Add the Java home directory to jenv
if jenv add "$path"; then
echo "Scrript Added $path to jenv."
else
echo "Failed to add $path to jenv."
fi
done
echo "Updated jenv versions:"
echo "The following paths:"
for path in "${java_home_paths[@]}"; do
echo "$path"
done
echo "...map to the following versions. (short numbers included)"
jenv versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment