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

355 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 23 Feb 2024
Edited: MathWorks Support Team on 23 Feb 2024
maci64 (Intel)
In MATLAB R2013a and earlier, MATLAB uses the system-wide JVM. In releases R2013b through R2019b, MATLAB uses the JVM that is bundled with MATLAB. Since R2020a, MATLAB also supports OpenJDK Java 8. Since R2023a, MATLAB supports OpenJDK Java 8 and 11.
maca64 (Apple Silicon)
Since R2023b, native Apple silicon MATLAB requires a Java runtime be installed on your Mac. Amazon makes a suitable Java 8 JRE available free of charge as part of Amazon Corretto 8. For more information, see MATLAB on Apple Silicon Macs.
MATLAB is only fully supported for these specified 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. You can also use MATLAB_JAVA environment variable approach as documented below. The setting by MATLAB_JAVA takes higher precedence over jenv/matlab_jenv when both of them are set. 
  • 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:
/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 Operating Systems/Platforms 

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

 

Windows
Linux
  1 Comment
DGM
DGM on 15 Mar 2024 at 23:06
Posted as a comment-as-flag by James McDaniel :
I have not been able to follow these instructions. Why can't this be done by the MATLAB installer?

Sign in to comment.

More Answers (0)

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!