Is it possible to install JDK 7 after installing MATLAB on Mountain Lion

I need to install MATLAB using an internet connection, but according to the docs I can't install MATLAB with a connection if I have JDK 7 installed on Mountain Lion. According to MATLAB, the solution is:

1) Install/activate manually without using Internet. MATLAB will work fine even if you have JDK 7 installed because it will still be using Apple's Java 6 that does not get removed when JDK 7 is installed.

2) Alternatively, you may uninstall JDK 7 which will let you install and activate MATLAB using Internet.

I need an internet connection, so number 1 won't work. I need JDK 7 as well, so I was wondering if I would be able to uninstall JDK 7, then install MATLAB, then reinstall JDK 7 without breaking MATLAB. Is this possible, or are there any other workarounds?


Solution 1:

Short answer: Yes. It'll work fine if you put JDK 7 back after the installation. Though you might have to do the same thing if you decide to de-activate Matlab at some point.

You've got a few options.

Option A: Use a newer Matlab

An easy fix could be to use Matlab R2014a. It supports Mavericks and comes with its own bundled JVM so it doesn't use the system JVMs. I just installed it on my Mavericks machine last week, which didn't even have Apple's Java 1.6 installed, and it worked fine. (I also looked at the source to confirm its "Activate" uses the bundled JRE.)

Option B: Temporarily Uninstall JDK 7

If that's not an option, it sounds like removing the JDK 7 is only needed during the installation process. I'd guess because it shells out to the default java for the software activation program instead of using the Matlab app's normal JRE detection and embedding logic. "Activate MATLAB" is a separate Java program, not part of the Matlab executable, so it's run using java. So you can put JDK 7 back once Matlab is installed; normal Matlab execution won't use it.

You can conveniently temporarily uninstall the JDK 7 by just moving it out of the /Library/Java directory so the system doesn't see it. Then you don't have to go through the whole download and installation process again.

cd /Library/Java/JavaVirtualMachines
mkdir ~/disabledJVMs
sudo mv jdk1.7.0_51.jdk ~/disabledJVMs
% also change the JAVA_HOME definition if you set it in an rc file or /etc/launchd.conf

% ... now install Matlab ...

% And restore the JDK
sudo mv ~/disabledJVMs/jdk1.7.0_51.jdk .

You can confirm the uninstallation worked with /usr/libexec/java_home.

Option C: Hack the Matlab Installer!

If you want to get fancy, you could actually hack the Matlab installer to force it to use Apple's JDK 1.6 for activation.

During the installation process, once all the files are copied in to /Applications but before you launch the Activation dialog, do this:

  • Go in to /Applications in Finder, right-click MATLAB, and choose "Show Package Contents".
  • Start an admin-enabled text editor from the Terminal with sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit.
  • Open bin/activate_matlab.sh in that TextEdit. Find where it's setting the paths to java and hardcode them to point to the Apple 1.6 JDK, which is in /System/Library/Java/JavaVirtualMachines/1.6.0.jdk. In the R2014a activate_matlab.sh, it's in functions setVMenvironment and setVMpath, and it sets up a JRE_HOME variable, which needs to be set to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home.
  • Do the same to deactivate_matlab.sh

And then you can proceed to the software activation step in the Matlab installer you left running, and I bet it will work even with JDK 7 in place.