Change java version (Mac)

Grains picture Grains · Sep 27, 2013 · Viewed 16.1k times · Source

I have 2 java versions on my computer:

/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

The 1.6.0 is set to default. How can I make my java programs to run 1.7?

Tried to add:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_07.jdk/Contents/Home

to my .zshrc file. But this seems to only change the path for my terminals java command.

Also tried to change the HOME symlink like this:

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

This had no effect at all.

Also tried java changer software: http://www.guigarage.com/2013/02/change-java-version-on-mac-os/ But no effect.

Any idea how to start java programs like .app and .jar files with the 1.7 version by just clicking on them?

Answer

Nicolas Rinaudo picture Nicolas Rinaudo · Sep 27, 2013

I believe OS X (at least 10.8) uses the following paths:

  • JRE: /System/Library/Frameworks/JavaVM.framework/Versions/Current
  • JDK: /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

Those are symlinks, which you can update to point to your 1.7 installation.

You can verify this fairly easily:

a) run which java to check which java executable is being executed. In theory, that should be /usr/bin/java.

b) run ls -la on your java executable, which should tell you where it points (/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java on my machine).

I think this should sort your .jar execution issue. If your Java application is wrapped in a .app, I believe it's a bit more complex: if memory serves, the version of java used will depend on the JavaApplicationStub being used by the .app.