MATLAB Answers

3

How do I change the Java Virtual Machine (JVM) that MATLAB is using for Mac OS?

Asked by MathWorks Support Team on 3 Oct 2013
Latest activity Edited by ACN
on 24 Feb 2015

I would like to use a different version of JVM than the default used in MATLAB for Mac OS.

11 Answers

Answer by MathWorks Support Team on 21 May 2015
 Accepted Answer

MATLAB ships with one specific version of the JVM software for MATLAB Releases R2013b (8.2) and later on Mac OS® X operating systems  .  For MATLAB Releases R2013a (8.1) and earlier on Apple Mac OS® X operating systems MATLAB uses the JVM that is available for all applications.

 

MATLAB is fully supported only on these shipped versions of the JVM software. Some components might not work properly under a different version of the JVM software. For example, calling functions in a dynamically linked library that was created with a different JVM software version than that used by MATLAB might cause a segmentation violation error message.

 

If you choose to change the JVM software version used by MATLAB, you must:

 

1.  Download and install the desired version of the JRE (Java Runtime Environment) or the JDK (Java Development Kit).  The latter contains the JRE and development tools.

2.  Determine the path of the JRE directory within that installation.  This varies by platform and whether you downloaded the JRE or JDK (see below).

3.  Set the MATLAB_JAVA environment variable to this path (see below).

4.  Start MATLAB while that environment variable is set (see below).

This article applies only to MATLAB launched from a shell window or modified shortcut as both of these methods set-up the MATLAB_JAVA variable.  Clicking on the regular MATLAB icon will not use the new Java.

Setting MATLAB to use new Java

Using shortcut:

As an alternative to using the Terminal to start MATLAB using the new Java, you can download the attached "createMATLABShortcut" script and run it in MATLAB. This will create a shortcut on the Desktop to start MATLAB with the updated JRE. By default it will use the JRE from:

 

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

This new shortcut can be dragged onto the Dock for easier access.  

Using Terminal:

If you would rather use Terminal to start MATLAB using the new Java, follow these steps:

Use the Finder to locate the JRE path. The JRE path will be similar to one of the following, depending on whether you have installed the JDK or just the JRE:

For JDK:

 

/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre

For JRE:

 

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Note that if the JRE path contains spaces as in the 2nd case above, you must enclose it in quotes in the steps below.

 

Open a window in Terminal and execute:

 

echo $SHELL

 

to determine what shell you are using.

  On csh or tcsh shells, use the "setenv" command to set the MATLAB_JAVA environment variable. For example:

 

setenv MATLAB_JAVA <JRE path>

 

This command needs to be run in every new shell window.

 

On the bash shell, execute:

 

export MATLAB_JAVA=<JRE path>

 

This setting will apply to MATLAB launched from that shell window.

 

Verifying the Installation

 

To verify that MATLAB is using the correct version of the JVM software, type the following at the MATLAB prompt:

 

>> version -java

 

If MATLAB is not using your value of MATLAB_JAVA, you may need to logout and log back in again.

 

 

 

Other OS:

 

To change the JVM MATLAB uses for other systems, use the following links.

Windows OS:

http://www.mathworks.com/matlabcentral/answers/130359-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-windows-os

Linux OS:

http://www.mathworks.com/matlabcentral/answers/130360-how-do-i-change-the-java-virtual-machine-jvm-that-matlab-is-using-for-linux-os

 

 

  2 Comments

This works, but there is a typo. The 2nd "echo" line should read:

echo 'export MATLAB_JAVA='<JRE path>'' >> ~/Desktop/myMATLAB

This solution makes the text usable for OSX 10.9 with retina display, however, when zooming in on plots it makes it a painful experience by freezing matlab momentarily before completing the zoom. Launching matlab without this fix does not have the zoom issue, but the font is blurry on a retina display.

Mathworks--Any tips on how to make the font not blurry and have figures fully functional? I know it's not a Java Heap size issue as I've allocated a hefty amount of memory for this during testing.

Log in to comment.


Answer by Carlos
on 6 Dec 2013
Edited by Carlos
on 6 Dec 2013

There's a missing bit in the answer above, which is how do get the MATLAB_JAVA setting to stick after a reboot and work with the GUI version.

Following the advice in a StackExchange answer, the way to do this is under OS X (I'm using Mavericks, but it might work under previous releases as well):

1) Create a new file called called matlab.props.plist under /Library/LaunchDaemons (I don't think the name is important as long as it ends in .plist):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>matlab.props</string>
    <key>ProgramArguments</key>
    <array>
        <string>launchctl</string>
        <string>setenv</string>
        <string>MATLAB_JAVA</string>
        <string>/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>LaunchOnlyOnce</key>
    <true/>
</dict>
</plist>

2) Replace the /Library/Internet... etc string with the path to the location of the JRE you have installed, if different.

3) Save the file.

4) Reboot.

5) Check the results by either doing 'launchctl getenv MATLAB_JAVA' in a terminal and/or 'version -java' from within MatLab.

  1 Comment

Log in to comment.


Answer by Jakob
on 27 Jan 2014

To start Matlab without blurry fonts under OS 10.9 Mavericks without adding any configuration files:

  1. Download the Java Runtime Environment
  2. Start Matlab by copy+pasting this in the terminal:
MATLAB_JAVA="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"; open /Applications/MATLAB_R2013b.app/ons/MATLAB_R2013b.app/

Answer by sinkyh
on 2 Dec 2013
Edited by sinkyh
on 3 Dec 2013

Is this solution still valid? I have installed the 1.7 JDK successfully and OSX 10.9 reports it as the default version being used:

 $ java -version
 java version "1.7.0_45"
 Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
 Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

I have followed these instructions and set the MATLAB_JAVA environment variable to:

/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre

However, running version -java in Matlab (R2013a) returns the old 1.6:

Java 1.6.0_65-b14-462-11M4609 with Apple Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode

Any suggestions? Or has this solution become not possible?

Thanks!

  0 Comments

Log in to comment.


Answer by Arturo
on 11 Dec 2013

I tried to put in the startup.m file

setenv('MATLAB_JAVA', '/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre')

then added the .plist file with the above path. After rebboting, I still didn't get any result and I decided to directly set it from the prompt shell with :

launchctl setenv MATLAB_JAVA /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre

then typed :

launchctl getenv MATLAB_JAVA 

getting the jre path. However, after the reboot the above command returns a null result.

Regards, Arturo

  1 Comment

It is not possible to set MATLAB_JAVA in a startup.m file, since the variable needs to be set before MATLAB launches.

Log in to comment.


Answer by Andrew Fernandes on 13 Jan 2014

On Mac OS 10.9, you can put launchd commands in /etc/launchd-user.conf. Mine has

setenv JAVA_HOME   /Library/Java/Home
setenv MATLAB_JAVA /Library/Java/Home/jre

with symlinks set appropriately. (I develop native Mac GUI apps for a living, and can verify that all processes properly inherit these environment variables. I've tested it.

Unfortunately, I have many native java apps on my system, and they all work with this environment setup... all except matlab.

Starting Matlab 2013a:

Failed to create resources from application bundle.  Using Java-based resources.
java.lang.NullPointerException
	at java.io.File.<init>(File.java:222)
	at com.apple.resources.LoadNativeBundleAction.run(MacOSXResourceBundle.java:60)
	at com.apple.resources.LoadNativeBundleAction.run(MacOSXResourceBundle.java:33)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.apple.resources.MacOSXResourceBundle.getMacResourceBundle(MacOSXResourceBundle.java:29)
	at com.apple.resources.MacOSXResourceBundle.getMacResourceBundle(MacOSXResourceBundle.java:24)
	at com.apple.laf.AquaLookAndFeel.initResourceBundle(AquaLookAndFeel.java:244)
	at com.apple.laf.AquaLookAndFeel.initComponentDefaults(AquaLookAndFeel.java:260)
	at com.apple.laf.AquaLookAndFeel.getDefaults(AquaLookAndFeel.java:227)
	at javax.swing.UIManager.setLookAndFeel(UIManager.java:520)
	at javax.swing.UIManager.setLookAndFeel(UIManager.java:564)
	at com.mathworks.mwswing.plaf.PlafUtils.setLookAndFeel(PlafUtils.java:296)
	at com.mathworks.mwswing.MJStartupForDesktop.init(MJStartupForDesktop.java:58)
	at com.mathworks.mwswing.MJStartupForDesktop.<clinit>(MJStartupForDesktop.java:32)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:171)
	at com.mathworks.util.PostVMInit$StartupClass.init(PostVMInit.java:96)
	at com.mathworks.util.PostVMInit.perform(PostVMInit.java:114)
>> getenv('MATLAB_JAVA')
ans =
/Library/Java/Home/jre
>> version -java
ans =
Java 1.6.0_65-b14-462-11M4609 with Apple Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode

From the command-line (or any GUI app):

> java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

  1 Comment

For R2013a and earlier, MATLAB looks for Java in /System/Library/ and expects Java 6 on Mac.

Log in to comment.


Answer by Alex
on 22 Feb 2014

Hi everyone. A few questions:

1.) So what about Matlab 2013b, does it also NOT come shipped with its own Java?

2.) If that's the case, do I NEED to have 64 bit JVM installed on my computer in order to use 64bit Matlab? (I realize this is most likely a stupid question but just double checking.)

3.) If the answer to #1 is, "No, it did come shipped with its own JVM", then how do I set Matlab to use its own JVM instead of trying to use the one installed with the OS?

*****

I just installed matlab 2013b, and this is the error I am getting:

****

Clearly, Matlab is looking in the wrong place, because my Java (32 bit) is actually installed in Program Files (x86). But if Matlab ships with its own Java, how do I make it use its own instead?

Thanks!

  2 Comments

MATLAB comes with Java on Windows and Linux for a long time/as long as I am aware of. R2013b was the first MATLAB release in which Java was shipped for Mac. Did you get the error message the first time you started R2013b? It might be worth contacting technical support.

"Please check if you have....".

It looks like you do - and it points to a corrupted/non-existent Java installation.

Delete the MATLAB_JAVA environment variable to get MATLAB to use the bundled JRE or correct the path to use your own.

Log in to comment.


Answer by Carlos
on 26 Feb 2014

Just to add to this, after using the stock java for a while I noticed frequent 'beach balling' in OS X when zooming in figures. So it seems like the option here is to have sharp fonts (you know, like it's the XXI century) or having non-freezing program.

It's just amazing that after all these months Mathworks hasn't ported the java fixes back to their version and released a fix.

  1 Comment

I doubt MATLAB will update the bundled JRE version in any release. They have not done so AFAIK with the Windows distro which has bundled a JRE for some time.

On OS X, you can simply replace the JRE in the MATLAB app package with your desired JRE. Both Java 7 updates and recent Java 8 early releases seem to work OK (but 8 is unsupported by TMW and there seem to be some issues).

Log in to comment.


Answer by Rasmus Ischebeck on 14 Mar 2014

I am using MATLAB R2014a on MacOS 10.9 Mavericks, installed on a MacBookPro with retina display. I have Java 1.7.0_51-b13 installed on my system.

Setting the environment variable MATLAB_JAVA in a terminal, and starting MATLAB from this terminal worked, but I wanted this change also to apply on the GUI that I start by clicking on the MATLAB application with the mouse.

Since none of the solutions described here worked for me, I did the following:

I edited the file

/etc/launchd-user.conf 

(as root), and included the line:

setenv MATLAB_JAVA /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

Note the backslash to make the space in the folder name work. (Quotes in this file do not seem to do the job.) I am using Lucida Sans Typewriter 9 point, and it renders beautifully with full resolution on the retina display.

  1 Comment

This did not work for me. The backslash does not seem to fix the folder name's space. Instead, I followed this suggestion, with "JAVA_OPTS" and "-Djava.io.tmpdir=/tmp -Dfile.encoding=UTF-8" replaced with "MATLAB_JAVA" and "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home". Now R2014a opens from the mac finder.

Log in to comment.


Answer by Warren
on 13 Jun 2014

Hi All,

After trying every available fix out there for this issue with no success, I think I came up with a very good answer for someone who likes to avoid the terminal (like me). It goes as follows:

MATLAB Retina Fix

  • Install the latest version 7 of java SDK
  • Find this install file, it should be located in /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home
  • In this Home folder, there will be a folder named JRE, copy it
  • Now find the matlab app, it should be under applications, which is accesible through Macintosh HD > Applications
  • Right click (Command Click), and choose show package contents.
  • Go to the folder located in sys > java > jre > maci64. There should be a folder labeled jre here, delete it (leave it in your trash can for now incase this does not work)
  • Now paste the SDK’s jre folder (the one you previously copied) to this location
  • Open matlab, it should have the updated java runtime, which is verifiable by typing: version -java in the command line

Hope this helps, I am a novice when it comes to using the terminal, so to fix this problem by just copy-pasting seems much simpler

  2 Comments

This worked, but I had to modify the process a little. The Oracle Java installer pointed to in the bug report that brought me here gives the JRE, not JDK, and it installs in:

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

Copy the contents of this folder into:

/Applications/MATLAB_R2014a.app/sys/java/jre/maci64/jre

(which should contain the same set of files before you copy). As suggested above, you might want to move the MATLAB JRE to the trash first, rather than copy over it, just in case things go bad.

This worked for me. Here is the sequence of shell commands that I used. Please modify the stuff in brackets.

First, we want to backup the original jre in Matlab (i.e. rename it)

cd /Applications/MATLAB_[VERSION NAME HERE].app/sys/java/jre/maci64/
mv jre jre.bak

Then we want to symlink the newer version of the JRE into this directory. Replace the first path with the location of your desired JRE folder. For example:

ln -s /Library/Java/JavaVirtualMachines/[JRE NAME]/Contents/Home/jre ./jre

Now launch Matlab. Should work.

Log in to comment.


Answer by Jack
on 20 Jun 2014
Edited by Jack
on 20 Jun 2014

Hi. I have this problem in Ubuntu when I replace contanining of matlab path jre with 1.6 version of jre. what should I do ? I want downgrade from 1.7 to 1.6. When I run matlab R2014a I have this :

Fatal Error on startup: Cannot locate or initialize class com/mathworks/jmi/OpaqueJavaInterface. java.lang.UnsupportedClassVersionError: com/mathworks/jmi/OpaqueJavaInterface : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Thanks.

  0 Comments

Log in to comment.