How do I change the Java Virtual Machine (JVM) that MATLAB is using on macOS?

206 views (last 30 days)
How do I change the Java Virtual Machine (JVM) that MATLAB is using on macOS?

Accepted Answer

MathWorks Support Team
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.
  • 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.
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:
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.
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.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
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.

Sign in to comment.

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"
<plist version="1.0">
<string>/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home</string>
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.
Mohammad Jafarnejad
Mohammad Jafarnejad on 25 Jun 2021
I think it does not work in Big Sur after this modification for bash shell:
<string>MATLAB_JAVA</string> <string>/Library/Java/JavaVirtualMachines/jdk1.7.0_40.jdk/Contents/Home/jre</string>

Sign in to comment.

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:
  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/

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:
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?

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

Sign in to comment.

Andrew Fernandes
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.
at Method)
at javax.swing.UIManager.setLookAndFeel(
at javax.swing.UIManager.setLookAndFeel(
at com.mathworks.mwswing.plaf.PlafUtils.setLookAndFeel(
at com.mathworks.mwswing.MJStartupForDesktop.init(
at com.mathworks.mwswing.MJStartupForDesktop.<clinit>(
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(
at com.mathworks.util.PostVMInit$StartupClass.init(
at com.mathworks.util.PostVMInit.perform(
>> getenv('MATLAB_JAVA')
ans =
>> 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
Malcolm Lidierth on 16 Jan 2014
For R2013a and earlier, MATLAB looks for Java in /System/Library/ and expects Java 6 on Mac.

Sign in to comment.

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?
Malcolm Lidierth
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.

Sign in to comment.

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

Sign in to comment.

Rasmus Ischebeck
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
(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 " -Dfile.encoding=UTF-8" replaced with "MATLAB_JAVA" and "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home". Now R2014a opens from the mac finder.

Sign in to comment.

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

Sign in to comment.

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( at java.lang.ClassLoader.defineClass( at at at$000( at$ at Method) at at java.lang.ClassLoader.loadClass( at sun.misc.Launcher$AppClassLoader.loadClass( at java.lang.ClassLoader.loadClass(


Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!