First run /usr/libexec/java_home -V which will output something like the following:
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
Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:
export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
or you can specify just the major version, like:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Now when you run java -version you will see:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
Add the export JAVA_HOME… line to your shell’s init file.
For Bash (as stated by antonyh):
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
For Fish (as stated by ormurin)
set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
Updating the .zshrc file should work:
nano ~/.zshrc
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
Press CTRL+X to exit the editor Press Y to save your changes
source ~/.zshrc
echo $JAVA_HOME
java -version
Answer from markhellewell on Stack OverflowHello!
I'm currently using Java 8 on my Mac and i'm finally getting around to the latest Java. I just installed JDK 19, and i'm wondering how i'm suppose to switch to the new jdk on my machine.
One doc says I can run this command: /usr/libexec/java_home -v 19 --exec javac -version
But my output just shows:
javac 1.8.0_291
Is there a standard way of switching java versions on Mac?
Switching versions of Java on Mac OSX
How do I switch between Java versions?
How to choose between multiple JDK's on MacOS?
Mac/PC Cross Platform?
Videos
First run /usr/libexec/java_home -V which will output something like the following:
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
Pick the version you want to be the default (1.6.0_65-b14-462 for arguments sake) then:
export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
or you can specify just the major version, like:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Now when you run java -version you will see:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
Add the export JAVA_HOME… line to your shell’s init file.
For Bash (as stated by antonyh):
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
For Fish (as stated by ormurin)
set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
Updating the .zshrc file should work:
nano ~/.zshrc
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8.0)
Press CTRL+X to exit the editor Press Y to save your changes
source ~/.zshrc
echo $JAVA_HOME
java -version
This answer is an attempt to address: how to control java version system-wide (not just in currently running shell) when several versions of JDK are installed for development purposes on macOS El Capitan or newer (Sierra, High Sierra, Mojave). As far as I can tell, none of the current answers do that (*).
As a developer, I use several JDKs, and I want to switch from one to the other easily. Usually I have the latest stable one for general use, and others for tests. But I don't want the system (e.g. when I start my IDE) to use the latest "early access" version I have for now. I want to control system's default, and that should be latest stable.
The following approach works with Java 7 to 12 at least (early access at the time of this writing), with Oracle JDK or OpenJDK (including builds by AdoptOpenJDK produced after mid-October 2018).
Solution without 3rd party tools:
- leave all JDKs at their default location, under
/Library/Java/JavaVirtualMachines. The system will pick the highest version by default. - To exclude a JDK from being picked by default, rename its
Contents/Info.plisttoInfo.plist.disabled. That JDK can still be used when$JAVA_HOMEpoints to it, or explicitly referenced in a script or configuration. It will simply be ignored by system'sjavacommand.
System launcher will use the JDK with highest version among those that have an Info.plist file.
When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).
Details of investigation in this gist.
(*) Current answers are either obsolete (no longer valid for macOS El Capitan or Sierra), or only address a single JDK, or do not address the system-wide aspect. Many explain how to change $JAVA_HOME, but this only affects the current shell and what is launched from there. It won't affect an application started from OS launcher (unless you change the right file and logout/login, which is tedious). Same for jenv, it's cool and all, but as far as I can tell it merely changes environment variables, so it has the same limitation.
I don't think it's possible to switch JRE (runtime environments) see here:
http://docs.oracle.com/javase/7/docs/webnotes/install/mac/mac-jre.html
which states:
Only one JRE can be installed. Installing a JRE removes the previously installed JRE. The JRE version used by the system can be determined in one of two ways:
Workaround:
I had a similar problem like you have with Minecraft with Wuala. Where I needed to run Wuala using Java 1.6 whilst I needed to develop with JDK 1.7 and I managed this by opening the Wuala.app package and changing its startup script in:
/Applications/Wuala.app/Contents/MacOS/wuala
from:
exec java ${VMARGS} -cp "${JAR_DIR}/loader3.jar":/System/Library/Java/ com.wuala.loader3.Loader3 -alternateprogrampath "${JAR_DIR}" -installed $*
to:
/usr/libexec/java_home -v 1.6.0 --exec java ${VMARGS} -cp "${JAR_DIR}/loader3.jar":/System/Library/Java/ com.wuala.loader3.Loader3 -alternateprogrampath "${JAR_DIR}" -installed $*
I.e simply replacing: exec with: /usr/libexec/java_home -v 1.6.0 --exec
This is of course rather involved and will get broken every time wuala autoupdates but otherwise it works.
To use another JDK see here:
http://docs.oracle.com/javase/7/docs/webnotes/install/mac/mac-jdk.html
which states:
To run a different version of Java, either specify the full path, or use the java_home tool:
/usr/libexec/java_home -v 1.7.0_06 --exec javac -version
Here is an illustration and examples from my setup:
Oracle JDK installs:
odin:~ geff$ ls -al /Library/Java/JavaVirtualMachines
total 0
21058660 0 drwxr-xr-x 3 root wheel - 102 24 Oct 18:04:33 2012 jdk1.7.0_09.jdk/
21061692 0 drwxr-xr-x 3 root wheel - 102 24 Oct 18:06:08 2012 jdk1.7.0_07.jdk/
21042328 0 drwxrwxr-x 3 root wheel - 102 20 Apr 06:58:53 2012 1.7.0.jdk/
21031664 0 drwxrwxr-x 7 root admin - 238 24 Oct 18:04:16 2012 ../
21042327 0 drwxr-xr-x 5 root wheel - 170 24 Oct 18:06:13 2012 ./
Apple supplied JDK:
odin:~ geff$ ls -al /System/Library/Java/JavaVirtualMachines
total 0
21026468 0 drwxr-xr-x 3 root wheel - 102 1 Nov 17:49:02 2011 1.6.0.jdk/
21026436 0 drwxr-xr-x 6 root wheel - 204 24 Mar 23:04:06 2012 ../
21026467 0 drwxr-xr-x 3 root wheel - 102 1 Nov 17:49:02 2011 ./
This works for me also to use the Apple supplied 1.6 JDK
odin:~ geff$ /usr/libexec/java_home -v 1.6.0_37 --exec java -version
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode)
Choose between the 1.7 Oracle versions:
Selecting the first Oracle JDK 1.7.0_04
odin:~ geff$ /usr/libexec/java_home -v 1.7.0_04 --exec java -version
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b21)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
Selecting JDK 1.7.0_07
odin:~ geff$ /usr/libexec/java_home -v 1.7.0_07 --exec java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
The default JDK is the highest one:
odin:~ geff$ java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
Defaults to the highest "patch level when using only minor version number:
odin:~ geff$ /usr/libexec/java_home -v 1.7.0 --exec java -version
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
One can use the java_home mechanism more conveniently.
Say you have Java 7 as the default:
$ java -version
java version "1.7.0_15"
…
Now let’s activate Java 6:
$export JAVA_HOME=$(/usr/libexec/java_home -v 1.6)
And there it is:
$ java -version
java version "1.6.0_41"
…
I added these to my .bashrc in order to make things even more convenient:
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
}
java_ls reduces the output of java_home -V to just the version numbers. And java_use is just a shortcut for what we did above. E.g. java_use 1.6 will switch to Java 6.