Matlab engine crashes JVM

11 次查看(过去 30 天)
Chris Rygaard
Chris Rygaard 2022-10-8
When calling Matlab from JAVA, the Matlab engine libraries crash the JVM. This Java code will reproduce the error:
import com.mathworks.engine.MatlabEngine;
import java.lang.invoke.MethodHandles;
import java.util.concurrent.Future;
public class TestBed
{
public static void main( String arguments[] )
{
Future<MatlabEngine> matlabEngineFuture;
MatlabEngine matlabEngine;
Future<Object> variableFuture;
Future<Void> voidFuture;
try
{
matlabEngineFuture = MatlabEngine.startMatlabAsync();
matlabEngine = matlabEngineFuture.get();
// Experiment 1:
//matlabEngine.eval( "MyStructure1.child( 1, 1 ) = 1;" );
//matlabEngine.eval( "MyStructure1.child( 1, 2 ) = 1;" );
//matlabEngine.eval( "MyStructure1.child( 2, 1 ) = 1;" );
//matlabEngine.eval( "MyStructure1.child( 1, 2 ) = 1;" );
//
//System.out.println( "Before retrieving MyStructure1" );
//
//variableFuture = matlabEngine.getVariableAsync( "MyStructure1" );
//variableFuture.get();
//
//System.out.println( "After retrieving MyStructure1" );
//
//voidFuture = matlabEngine.fevalAsync( 0, "clear", "MyStructure1" );
//System.out.println( "voidFuture = " + voidFuture );
//voidFuture.get();
// Experiment 2:
//matlabEngine.eval( "MyStructure2.child( 1 ).child1 = 1;" );
//matlabEngine.eval( "MyStructure2.child( 2 ).child1 = 1;" );
//
//System.out.println( "Before retrieving MyStructure2" );
//
//variableFuture = matlabEngine.getVariableAsync( "MyStructure2" );
//variableFuture.get();
//
//System.out.println( "After retrieving MyStructure2" );
//
//voidFuture = matlabEngine.fevalAsync( 0, "clear", "MyStructure2" );
//voidFuture.get();
// Experiment 3:
//matlabEngine.eval( "MyStructure3.child1.child1 = 1;" );
//matlabEngine.eval( "MyStructure3.child2.child1 = { 'testString1', 'testString2' };" );
//
//System.out.println( "Before retrieving MyStructure3" );
//
//variableFuture = matlabEngine.getVariableAsync( "MyStructure3" );
//variableFuture.get();
//
//System.out.println( "After retrieving MyStructure3" );
//
//voidFuture = matlabEngine.fevalAsync( 0, "clear", "MyStructure3" );
//voidFuture.get();
// Experiment 4:
matlabEngine.eval( "MyStructure4.child( 1, 1 ).child1 = 1;" );
matlabEngine.eval( "MyStructure4.child( 1, 2 ).child1 = 1;" );
matlabEngine.eval( "MyStructure4.child( 2, 1 ).child1 = 1;" );
matlabEngine.eval( "MyStructure4.child( 1, 2 ).child1 = 1;" );
System.out.println( "Before retrieving MyStructure4 (the JVM crashes here)" );
variableFuture = matlabEngine.getVariableAsync( "MyStructure4" );
variableFuture.get();
System.out.println( "After retrieving MyStructure4 (this is never displayed)" );
voidFuture = matlabEngine.fevalAsync( 0, "clear", "MyStructure4" );
voidFuture.get();
// ----------------------------------------------------------------------------------------
}
catch ( Exception exception )
{
System.out.println( "This is never displayed" );
exception.printStackTrace();
}
System.out.println( "This is never displayed" );
}
}
In order to bracket the problem, I tried a few other experiments. The segments that are commented out show my other experiments. They all run smoothly.
This is preventing me from progressing, so any help would be appreciated. I've run these on R2022a. On Windows, I merely get a cryptic JVM crash report. On Linux, the error message issued by the crashing JVM is less cryptic, something about "not enough arguments supplied".
Thanks,
Chris

回答(1 个)

Infinite_king
Infinite_king 2023-12-1
编辑:Infinite_king 2023-12-1
Hi Chris Rygaard,
I understand that you are trying to use MATLAB from JAVA through MATLAB Engine API and in some cases this is causing JVM to crash.
Follow the below troubleshooting steps to resolve the problem,
  1. Make sure the JAVA version is compatible with MATLAB. Refer the following MATLAB document to check the compatible versions, https://www.mathworks.com/support/requirements/language-interfaces.html
  2. Instead of using ‘variableFuture.get()’ try using ‘variableFuture.get(timeout, timeUnit)’ with a reasonable timeout value. This will help you identify if the issue is related to the retrieval process.
  3. Try to open the crash logs and see if you can locate the exact error for crashing. Refer the following document to locate crash files, https://www.mathworks.com/help/matlab/matlab_env/error-log-reporting.html
  4. Try to catch the following exceptions to see if you are encountering any expected exceptions, https://www.mathworks.com/help/matlab/matlab_external/trouble-shooting-matlab-engine-api-exceptions.html
  5. Make sure the syntax of expression that you are trying to evaluate using MATLAB is correct.
For more information on using MATLAB API from JAVA, refer the following MATLAB documentation,
Hope this is helpful.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by