Macos – How to switch between Java 7 and Java 6 on OS X 10.8.2

javamacososx-mountain-lion

I am using Java SE 7 to program simple games for windows and mac, but I have to switch to java 6 to run games such as Minecraft on my computer. This used to work just fine before I updated to Mac OS X 10.8.2 Build 12C60.

Before this update I used to be able to open Java Preferences and check off Java 6 or Java 7. But now I don't seem to be able to find Java Preferences.

I am running OS X 10.8.2 (latest version).

Best Answer

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)
Related Question