Skip to content

Instantly share code, notes, and snippets.

@noxone
Last active September 20, 2024 12:27
Show Gist options
  • Save noxone/4e9a8bb4778efcf9cea8ab0b13aef757 to your computer and use it in GitHub Desktop.
Save noxone/4e9a8bb4778efcf9cea8ab0b13aef757 to your computer and use it in GitHub Desktop.
Change JDK on MacOS

Change JDK on MacOS

Find all installed JDK's

Run /usr/libexec/java_home -V. This will show you all installed (and known) JDK's:

Matching Java Virtual Machines (3):
1.8.0_05, x86_64:   "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64:   "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home

Select another JDK

Pick the version you want to be the default. In this example 1.6.0_65-b14-462:

export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`

If you run java - version now, you will see the changed version string.

Tips

Specify major version

It is also possible to specify just the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Add to shell's init file

Add the export JAVA_HOME ... line to your shell’s init file.

For Bash

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

For Fish

set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)

.zshrc

Just add the following line to the file:

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

Sources

This gist is based on this stackoverflow question. It is basically the same content, but this way I don't need to seach for it every time again.

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