Skip to content

Instantly share code, notes, and snippets.

@amrynsky
Last active August 25, 2022 16:45
Show Gist options
  • Save amrynsky/eef394e7a8a15bf2412c36943dea76a1 to your computer and use it in GitHub Desktop.
Save amrynsky/eef394e7a8a15bf2412c36943dea76a1 to your computer and use it in GitHub Desktop.
Change default java version

First run /usr/libexec/java_home -V which will output something like the following:

Matching Java Virtual Machines (5):
    11, x86_64:	"OpenJDK 11"	/Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home
    10.0.2, x86_64:	"Java SE 10.0.2"	/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
    9.0.4, x86_64:	"Java SE 9.0.4"	/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
    1.8.0_152, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home
    1.8.0_111, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home

Pick the version you want to be the default (1.8.0_152, x86_64 for arguments sake) then:

export JAVA_HOME='/usr/libexec/java_home -v 1.8.0_152, x86_64' or you can specify just the major version, like:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

Just add the export JAVA_HOME… line to your shell’s init file.

@amrynsky
Copy link
Author

amrynsky commented May 27, 2019

Add a couple of aliases to your .profile like this to quickly switch java version

alias j9="export JAVA_HOME=/usr/libexec/java_home -v 9; java -version"
alias j8="export JAVA_HOME=/usr/libexec/java_home -v 1.8; java -version"
alias j11="export JAVA_HOME=/usr/libexec/java_home -v 11; java -version"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment