Apple - How can I switch between the two Java versions in Mountain Lion now that Java Preferences is gone?

Open up a terminal use this command:

/usr/libexec/java_home -v 1.6 

You can change the 1.6 to any version of java you want to use and the command will output the location of where that version of java resides. (i.e: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home)

then you can set the JAVA_HOME like you normally would with the output of the previous command. i.e.:

export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

So to simplify; something like this should work:

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

remember to check the version with:

java -version

Hope it helps.


You may need two approaches:

  • one to switch the version for the applet plug-in alone
  • another approach to switch the version for other parts of the JRE without switching the version for the applet plug-in.

My recent use case:

  • Java 8 for apps that do not use the plug-in
  • Java 7 Update 9 for the applet plug-in.

To switch the applet plug-in but not other parts of the JRE/JDK from Java 8 to Java 7 Update 9

  1. quit web browsers and any other apps that use Java or the Java applet plug-in
  2. install JDK 8
  3. set aside the plug-in
  4. install JRE 7 Update 9 or JDK 7 Update 09.

Setting aside: suggested move

sudo mv /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin /private/var/tmp

If you plan to switch frequently, you may prefer a different temporary location.

Result

sh-3.2$ java -version
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b58)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b02, mixed mode)
sh-3.2$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.8.2
BuildVersion:   12C60

At the same time:

Screenshot of Oracle's verification of Java version in Safari

To switch the applet plug-in but not other parts of the JRE/JDK from Java 7 Update 9 to Java 6 Update 37

Answers under:

  • Re-Enable Apple Java 6 SE from Command Line?

To switch the JRE but not the applet plug-in from Java 8 to Java 7 Update 9

Install JDK 7 Update 09 then JDK 8

  • the OS and apps will default to use the greatest version.

Quit any apps that use Java. No need to quit the browser.

Set aside the jdk1.8… directory from

/Library/Java/JavaVirtualMachines

  • the OS and apps will use Java 7 Update 9.

To switch the JRE but not the applet plug-in from Java 7 Update 9 to Java 6 Update 37

Quit any apps that use Java. No need to quit the browser.

Set aside the jdk1.7… directory from

/Library/Java/JavaVirtualMachines

  • the OS and apps will use Java 6 Update 37.

Example

macbookpro08-centrim:~ gjp22$ 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)
macbookpro08-centrim:~ gjp22$ sudo mv /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk /private/var/tmp
Password:
macbookpro08-centrim:~ gjp22$ 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)

Generally

For users of OS X, some of what's provided by Oracle is initially misleading:

  • Why does the version of Java verified by Oracle differ from the version shown at the command line?

… and so on.

Simplicity

Whilst none of the above is as simple as switching with the Java Preferences app previously installed by Apple, the relatively simple approach of setting aside (no need for special uninstallation) is reminiscent of the wish for drag-and-drop installation … previously expressed in the openjdk-osx-build area.

Someone might like to build an app, but I should not recommend using symlinks – it would work, but could be inconsistent with future drag-and-drop approaches to installation.

Side notes

At the time of writing, the most recent supported releases for which Oracle makes updates publicly available are (for platforms other than OS X) Java 6 Update 37 and (for platforms including OS X) Java 7 Update 9.

As greater releases will be made available, please reinterpret this answer accordingly.

Java version terminology in this answer is in line with Java Help Center terminology.


It looks like you'll have to do it manually. Oracle's documentation states that only one JRE can be installed at a time.

This Apple support page describes how to switch between the Apple SE 6 JRE and the Oracle SE 7 JRE. Briefly, to switch from Oracle's JRE back to Apple's, start the Terminal app and type the following:

sudo mkdir -p /Library/Internet\ Plug-Ins/disabled

sudo mv /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin /Library/Internet\ Plug-Ins/disabled

sudo ln -sf /System/Library/Java/Support/Deploy.bundle/Contents/Resources/JavaPlugin2_NPAPI.plugin /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin

There are more (and better) instructions on the support page on how to switch Web Start back to JRE 6, and how to switch everything back to Oracle JRE 7 (reinstall the JRE).

Not as simple as using the Java Preferences utility, but better than nothing.

Tags:

Java

Macos