How do I change my JDK version on Big Sur?

Viewed 943

Now that I'm on Big Sur, I can't set my jdk anymore! I used to do this:

$ /usr/libexec/java_home -v 15

Now it just responds like this:

$ /usr/libexec/java_home -v 15
/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home

Before Big Sur, this command would always respond with the jdk path for the version I specified. My .bash_profile file has these aliased defined:

alias j15="export JAVA_HOME=`/usr/libexec/java_home -v 15`; java -version"
alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_212`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"
j12

So it would start with Java 12, but I could just type j15 and it would set my jdk to 15. But now none of them work. I can type any of them, but it stays at JDK 12. (I have different projects which require different JDK versions.)

Is there a better way to do this now?

1 Answers

List all versions with Capital 'V':

/usr/libexec/java_home -V

Output on My machine:

Matching Java Virtual Machines (2):
    11.0.9 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_181 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

While selecting a specific version enclose it with quotes:

/usr/libexec/java_home -v "11.0.9"

Fix for setting or unsetting current JAVA_HOME is:

unset JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home -v "11.0.9")
Related