Last active
July 26, 2024 03:00
-
-
Save Esonhugh/2f448d0e9cf77acec388db7089394a35 to your computer and use it in GitHub Desktop.
update java versions on macos or other computer. This script will switch java version at current env once.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Author: Esonhugh | |
# Date: 2024/07/26 | |
# Java is installed on mac with brew and IDEA | |
# following path is the IDEA path and brew path | |
# JAVA Alternatives | |
export COMMON_JAVA_HOME="$HOME/Library/Java/JavaVirtualMachines" | |
use_java_8 () { | |
export JAVA_HOME="$COMMON_JAVA_HOME/azul-1.8.0_412/Contents/Home" | |
export __OLD_JAVA_PATH=$PATH | |
export PATH="${JAVA_HOME}/bin:$PATH" | |
} | |
use_java_11(){ | |
export JAVA_HOME="$COMMON_JAVA_HOME/azul-11.0.23/Contents/Home" | |
export __OLD_JAVA_PATH=$PATH | |
export PATH="${JAVA_HOME}/bin:$PATH" | |
} | |
use_java_18 () { | |
export JAVA_HOME="$COMMON_JAVA_HOME/azul-18.0.2.1/Contents/Home" | |
export __OLD_JAVA_PATH=$PATH | |
export PATH="${JAVA_HOME}/bin:$PATH" | |
} | |
use_java_22 () { | |
export JAVA_HOME="$COMMON_JAVA_HOME/openjdk-22.0.1/Contents/Home" | |
export __OLD_JAVA_PATH=$PATH | |
export PATH="${JAVA_HOME}/bin:$PATH" | |
} | |
use_java_unuse () { | |
if [ -z "${__OLD_JAVA_PATH}" ] | |
then | |
export PATH=${__OLD_JAVA_PATH} | |
unset __OLD_JAVA_PATH | |
fi | |
} | |
function use_java () { | |
if [ -z "$1" ] | |
then | |
if which "java" >/dev/null 2>&1 ; then | |
echo "Detected java in your PATH" | |
else | |
echo "java is not installed in your PATH. Checkit out" | |
fi | |
echo "inspect current java version" | |
echo "Java Binary is" `which java` | |
echo "JAVA_HOME is '$JAVA_HOME'" | |
echo "Execute java -version" | |
java -version||java --version | |
return 0 | |
fi | |
if test "$1" = "8" -o "11" -o "18" -o "22" -o "unuse" | |
then | |
if which "use_java_$1" >/dev/null 2>&1 ; then | |
echo "using java version $1" | |
use_java_$1 | |
else | |
echo "undefined java alias" | |
fi | |
else | |
echo "undefined java version" | |
fi | |
} | |
#use default java | |
use_java_8 | |
# you can add other alias there. | |
alias update-java-version="use_java" | |
alias jvm="use_java" # java version manager. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment