Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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).

share|improve this question
2  
You are correct, the Java Preferences is now removed with the last update. Some various shell methods I've tried are not working out, but there must be some mechanism to facilitate this when you're running the most recent 7 as well. –  ylluminate Oct 21 '12 at 20:50

9 Answers 9

up vote 28 down vote accepted

I don't think its 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$ ll /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$ ll /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)

regards Geff

share|improve this answer
    
Thank You this did change my JDK even though i had to change the "-v 1.6.0_37" to "-v 1.6.0_42" (the version i had downloaded) and it worked so thank you. –  Nikita Jerschow Apr 15 '13 at 1:31

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.

share|improve this answer
1  
in java_use you should remove previous java from $PATH. –  Eir Nym Feb 20 at 13:42

A solution for easily switching the JDK (not sure about JRE) (source):

function setjdk() {  
  if [ $# -ne 0 ]; then  
   removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'  
   if [ -n "${JAVA_HOME+x}" ]; then  
    removeFromPath $JAVA_HOME  
   fi  
   export JAVA_HOME=`/usr/libexec/java_home -v $@`  
   export PATH=$JAVA_HOME/bin:$PATH  
  fi  
  echo JAVA_HOME set to $JAVA_HOME  
  java -version  
 }  
 function removeFromPath() {  
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")  
 }

(add above function to your .bash_profile)

Usage:

$ setjdk 1.7
share|improve this answer
    
Can you include the relevant parts of the post here. Having the answer here means that if the link changes the answer is still useful. –  ChrisF Mar 18 '13 at 23:15
    
Updated the answer –  Neeme Praks Mar 19 '13 at 10:00

A quick workaround is to create a symlink to the old Java 6 binary:

sudo ln -s /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Commands/java /usr/local/bin/java6

Then run the apps with: java6 -jar myapp.jar

share|improve this answer

The Java preferences app doesn't seem to list Java 1.6 anymore. However when I check java version in the command line I get:

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)
share|improve this answer

It's pretty simple to switch Java versions instantly on OS X without needing to change the $JAVA_HOME variable.

I am running 10.8.2, and my $JAVA_HOME variable points to /Library/Java/Home

echo $JAVA_HOME

This path is a symbolic link, as can be seen by running ls -l command

ls -la /Library/Java

The default symbolic link points to

Home -> /Library/Java/JavaVirtualMachines/1.6.0_33-b03-424.jdk/Contents/Home/

To redirect java to my 1.7 installation, I merely replace the old Home symlink with a new symlink pointing to the home directory of my Java 1.7 installation.

cd /Library/Java
mv Home Home-1.6
ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home/ Home

And voila! java -version shows that I am now running java 1.7. Just switch which symlink is currently named 'Home' to instantly change the default java JRE.

share|improve this answer

Here is a easy way to switch java 6 and java 7 on mac osx.

step 1 : download jdk 7 from oracle official website http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html and install it step by step follow the tips.

step 2: set $JAVA_HOME in .bash_profile

$ vim .bash_profile 

export JAVA_HOME=$(/usr/libexec/java_home)

$ source .bash_profile

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

step 3: if you want to use jdk 6, just alternative $JAVA_HOME to jdk 6 home path

$ vim .bash_profile 

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

$ source .bash_profile

$ echo $JAVA_HOME
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
share|improve this answer

Based on the above and a bit of other research, I've devised a script you can "source" (or "dot") into your profile. It'll prompt you with a menu of available JAVA_HOMEs on your Mac, though you can also specify the default home, or pre-select a menu choice.

In my .bash_profile:

export PREJAVA_PATH="$PATH:~/bin"
export PATH="${PREJAVA_PATH}"
. gojava -d

Note it requires a variable PREJAVA_PATH. You can also call it from the command line:

$ . gojava

Or to pre-choose the second option:

$ . gojava -c 2

File gojava is:

#!/bin/bash

_exe=/usr/libexec/java_home
_defaultHome=$($_exe)

usage() {
cat <<EOF
$0 [-d] [-c menu_choice]

Sets JAVA_HOME and PATH variables based on a user choice from a menu of available JDKs.

Parameters:

  -d : use system default ($_defaultHome)
  -c : specify a menu choice in advance, without being prompted

Note that environment variable PREJAVA_PATH must be set, and should include the PATH that 
you want with everything but JAVA_HOME/bin.

EOF
exit 1
}

setHomePath() {
  export JAVA_HOME="$1"
  export PATH="${JAVA_HOME}/bin":"${PREPATH}"
}

# TODO: [enhancement] figure out a better way to determine a PATH that excludes the JAVA_HOME
if [ -z "${PREJAVA_PATH}" ]; then
  usage  
else
  PREPATH=${PREJAVA_PATH}
fi

_default=0
_choice=0
OPTIND=1
while getopts "dc:" name; do
 case $name in
      d) _default=1 ;;
      c) _choice="$OPTARG" ;;
      *) echo "unknown parameter $name"
         usage
         ;;
 esac
done

if [ $_default -eq 1 ]; then
  setHomePath "$_defaultHome"
else
  i=1
  while read -r line; do
    choiceList[$i]="$line"
    homeList[$i]=$(echo $line | sed 's/[^\/]*\(\/.*\)/\1/')
    i=$(($i+1))
  done < <($_exe -V 2>&1 | egrep -v '^$|^\/|^Matching')

  if [ $_choice -gt 0 -a $_choice -lt $i ]; then
    setHomePath "${homeList[$_choice]}"
  else
    printf "\nList of available Java versions:\n"
    select choice in "${choiceList[@]}"; do
      if [ $REPLY -gt 0 -a $REPLY -lt $i ]; then
        setHomePath "${homeList[$REPLY]}"
        break
      fi
    done
  fi
fi
share|improve this answer

Manage your Java Environment (on Mac and Linux): http://www.jenv.be/

Similar to RVM for Ruby and N for Node but for Java.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.