- For R2021b and later versions, use MATLAB's jenv or matlab_jenv command.
- For R2021a and earlier versions, set the MATLAB_JAVA environment variable to this path, and start MATLAB while that environment variable is set. See detailed instructions below. This method 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.
How do I change the Java Virtual Machine (JVM) that MATLAB is using on macOS?
216 views (last 30 days)
Show older comments
MathWorks Support Team
on 3 Oct 2013
Commented: CdC
on 29 Jan 2023
How do I change the Java Virtual Machine (JVM) that MATLAB is using on macOS?
Accepted Answer
MathWorks Support Team
on 6 May 2022
Edited: MathWorks Support Team
on 6 May 2022
On macOS, MATLAB R2013b and later use a JVM which is bundled with MATLAB. In MATLAB R2013a and earlier, MATLAB uses the system-wide JVM.
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 path to the JRE directory.
Setting MATLAB to use new Java (R2021a and earlier versions)
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/HomeNote 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:
Linux OS:
4 Comments
CdC
on 29 Jan 2023
Can you please explain how to get Matlab to work if you don't know what the status of your java environment on your Mac is? I shouldn't think that this level of technical sleuthing would be required to get a basic install of Matlab to work. My situation is that I have been successfully using R2022a. I recently installed R2022b. When I click it just doesn't run. When I run it from the terminal, it will run if I use the -nojvm flag. Otherwise, I get the error: Cannot locate or initialize class com/mathworks/jmi/OpaqueJavaInterface.
java.lang.NoClassDefFoundError: com/mathworks/jmi/OpaqueJavaInterface
Caused by: java.lang.ClassNotFoundException: com.mathworks.jmi.OpaqueJavaInterface
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
I do not know what the status of Java on my machine is, and so far this article does not appear to explain how to find out.
Thank you.
More Answers (10)
Carlos
on 6 Dec 2013
Edited: MathWorks Support Team
on 10 May 2021
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.
2 Comments
Mohammad Jafarnejad
on 25 Jun 2021
I think it does not work in Big Sur after this modification for bash shell:
<string>launchctl</string>
<string>export</string>
<string>MATLAB_JAVA</string> <string>/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre</string>
Jakob
on 27 Jan 2014
Edited: MathWorks Support Team
on 10 May 2021
To start Matlab without blurry fonts under OS 10.9 Mavericks without adding any configuration files:
- Download the Java Runtime Environment
- 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/
sinkyh
on 2 Dec 2013
Edited: 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
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
Kerri Keng
on 6 Jan 2014
It is not possible to set MATLAB_JAVA in a startup.m file, since the variable needs to be set before MATLAB launches.
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
Malcolm Lidierth
on 16 Jan 2014
For R2013a and earlier, MATLAB looks for Java in /System/Library/ and expects Java 6 on Mac.
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
Malcolm Lidierth
on 27 Feb 2014
"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.
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
Malcolm Lidierth
on 27 Feb 2014
Edited: Malcolm Lidierth
on 27 Feb 2014
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).
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
s
on 30 Mar 2014
Edited: MathWorks Support Team
on 10 May 2021
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.
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
ACN
on 24 Feb 2015
Edited: ACN
on 24 Feb 2015
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.
Jack
on 20 Jun 2014
Edited: 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
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!