MATLAB Language Debugging Debugging Java code invoked by MATLAB


Example

Overview

In order to debug Java classes that are called during MATLAB execution, it is necessary to perform two steps:

  1. Run MATLAB in JVM debugging mode.
  2. Attach a Java debugger to the MATLAB process.

When MATLAB is started in JVM debugging mode, the following message appears in the command window:

JVM is being started with debugging enabled.
Use "jdb -connect com.sun.jdi.SocketAttach:port=4444" to attach debugger.

MATLAB end

Windows:

Create a shortcut to the MATLAB executable (matlab.exe) and add the -jdb flag at the end as shown below:

enter image description here

When running MATLAB using this shortcut JVM debugging will be enabled.

Alternatively the java.opts file can be created/updated. This file is stored in "matlab-root\bin\arch", where "matlab-root" is the MATLAB installation directoy and "arch" is the architecture (e.g. "win32").

The following should be added in the file:

-Xdebug
-Xrunjdwp:transport=dt_socket,address=1044,server=y,suspend=n

Debugger end

IntelliJ IDEA

Attaching this debugger requires the creation of a "remote debugging" configuration with the port exposed by MATLAB:

Step 1: Creating a debug configuration

Then the debugger is started:

Step 2: Starting the debugger

If everything is working as expected, the following message will appear in the console:

Log output when all is working