Java-less MATLAB is coming
... but you can bring your own Java when needed.
Published: 2022-10-25

Summary
- Using the OpenJDK-8 JRE in MATLAB by setting it with MATLAB's jenv() function works well. We have tried it with a suite of large MATLAB-Java GUIs and so far have not found issues.
- Using newer OpenJDK JREs with jenv() does work, somewhat. The MATLAB desktop uses Java classes that require Java 8 to work. So, with a newer JRE you get a lot of Java related error messages in the MATLAB Command Window and many MATLAB desktop controls are not functional any more.
- Changing the heap space in the MATLAB preferences still results in the heap space of a new JVM to be set to the selected value.
- Debugging Java code with Java code in other editors works. A connection between MATLAB and the editor needs to setup via a port, as is the case when debugging with the Oracle JRE version that is in the installation.
- Tested on Windows with MATLAB releases R2021b and R2022b and OpenJDK builds from Eclipse Temurin.
The problem (?)
In the near future MATLAB may no longer contain a Java Runtime Environment (JRE) as part of its installation and undocumented Java classes created by the MathWorks will be removed. Interfacing with Java should remain possible, but we may have to bring our own JRE.
The MATLAB installation will also no longer contain all third party Java libraries it does now. We can add these easily ourselves. Most of these libraries are open source, but some may require a (paid) license.
These points were raised in the Release notes for MATLAB R2020b in the External Language Interfaces
section:
Java packages to be removed
Behavior change in future release
"Certain Java® libraries (JAR files) used by MathWorks products might not be included in future releases. To continue using functionality from these libraries, install the JAR files and add them to the class path used by MATLAB. For information about adding JAR files to the static class path, see > Static Path."
Undocumented com.mathworks Java packages to be removed
The MATLAB Code Compatibility Report lists com.mathworks Java classes as Unsupported functionality that might cause errors. Except for documented interfaces and classes in the com.mathworks.engine and com.mathworks.matlab.types packages (the MATLAB Engine API for Java) and the Java Client API for use with MATLAB Compiler SDK™ and MATLAB Production Server™, all other com.mathworks Java interfaces and classes are undocumented and might be modified or removed without warning in future releases. For code stability, avoid using any of these undocumented Java interfaces or classes.
And from the MATLAB website:
Java Packages to Be Removed
Java® packages and sub-packages will not be available in MATLAB® in a future release.
To continue using a Java package, install its JAR file and add the JAR file to the static path in MATLAB using the instructions in Static Path of Java Class Path.
Using another JRE
With the introduction of the jenv function in MATLAB R2021b it has become possible to make MATLAB use a JRE other than the Oracle build that is in the MATLAB installation.
The MATLAB installation currently contains Java 8, so many classes in the MATLAB installation are compiled for that Java version and may not work in newer Java versions.
OpenJDK 8
There are quite a few organisations that offer long term support OpenJDK builds with a permissive license. For our evaluation of the jenv()
functionality we have downloaded an OpenJDK build via the AdoptOpenJDK website.
We set the downloaded OpenJDK build to be used by MATLAB:
and restarted MATLAB to get a session using the OpenJDK-8 JRE. We quickly checked which JRE was used by MATLAB:
We have tested using the OpenJDK-8 build with MATLAB R2021b and R2022b on Windows with a suite of MATLAB-Java GUIs and have not found issues yet. In fact as we could not find any changes, we even checked whether MATLAB was really using the OpenJDK classes by running the following code:
p = javax.swing.JPanel();
c = p.getClass()
c.getResource([char(c.getSimpleName()), '.class'])
So, MATLAB was using the OpenJDK-8 JRE classes we made available with jenv()
and not the Oracle ones in the installation. Conclusion: using the OpenJDK-8 JRE in MATLAB works well and is a viable way of keeping MATLAB-Java code functioning.
OpenJDK > 8
With the jenv()
function we can also set MATLAB to use a JRE version that it was not designed for.
OpenJDK 11
From the same website as before we downloaded the OpenJDK-11 build for Windows.
the MATLAB desktop does start when the OpenJDK-11 build is set with jenv()
, but with warning and error messages printed in Command Window. Many controls (such as menus and buttons) in the GUI cause error messages to be printed and are no longer functional. This basically causes the MATLAB desktop to no longer be useful for developing MATLAB code, but you can continue to run code (by pressing the F5 key).
You can still use Java classes and libraries that are in the MATLAB installation as long as these do not depend on Java 8. For instance it is still possible to import XML files with Apache Xerces and create a simple Java GUI like in the following code snippet:
b = com.jidesoft.swing.JideSplitButton();
b.add('test');
f = javax.swing.JFrame();
f.add(b, []);
f.show()
Developing MATLAB code with a OpenJDK-11 build in a fully functional MATLAB desktop is not feasible as long as the MATLAB desktop depends on Java-8.
OpenJDK 18
When the OpenJDK-18 JRE is selected with the jenv()
function, the MATLAB desktop no longer starts. You can still start MATLAB with the -nodesktop
option though. This starts MATLAB without a desktop and allows you to run code from the command line.
The MATLAB installation contains a matlab_jenv
executable with which MATLAB can be reset to the use the Java JRE that is part of the MATLAB installation:
matlab_jenv factory
No MATLAB desktop
As mentioned before running matlab with the -nodesktop
option does work in combination with the OpenJDK builds. You can run MATLAB code and use Java libraries that are -for now- still in the MATLAB installation. You can for instance still load xml files with the apache xerxes library and get their Java DOM nodes representation in MATLAB.
Java heap preference
Changing the Java Heap setting in the MATLAB preferences still works. The preference is used when starting up the Java environment in a new MATLAB session:
If we set the Java Heap to 128 MB in the MATLAB preferences window:
and run the following code to get the Java startup options
java.lang.management.ManagementFactory.getRuntimeMXBean.getInputArguments()
we can see that in the Java startup options the maximum heap was indeed set to 128 MB:
Debugging Java
Java source code used in MATLAB can be debugged by starting MATLAB with the option -jdb portnumber
.
When you start MATLAB with the following command:
matlab -jdb 4444
you will see the following message in the MATLAB command window:
JVM is being started with debugging enabled.
Use "jdb -connect com.sun.jdi.SocketAttach:port=4444" to attach debugger.
When your Java IDE is configured to connect over the same localhost port to the MATLAB session, like this Eclipse workspace:
then the execution of the Java code is paused when a breakpoint is hit in the Java IDE:
We have also tested this for Visual Studio Code with the Extension Pack for Java
, so we are confident that debugging Java code is still possible with any Editor configured for debugging Java code.