Scripts to select the default Java version on MacOS

Thanks to superuser.com I found a neat trick to make it easy to list your installed Java versions and switch between them. Just add these to your .bashrc file:


#
#
#
alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'

function java_use() {
    export JAVA_HOME=$(/usr/libexec/java_home -v $1)
    export PATH=$JAVA_HOME/bin:$PATH
    java -version
}

The java_ls command reduces the output of java_home -V to just return the version numbers and java_use is just a shortcut for the exports and path updates you’d normally use.

Leave a Reply

Your email address will not be published. Required fields are marked *