MATLAB: Does MATLAB crash when I try to use Java classes from a JAR file in the dynamic Java path in MATLAB 7.0.1 (R14SP1)

classloaderclasspath.txtjavaMATLABsegv

I am calling Java code from MATLAB, including some dynamic Java classes. When I try to use classes from the JAR file, MATLAB crashes.

Best Answer

This bug has been fixed in Release 14 Service Pack 3 (R14SP3).
For previous product releases, read below for possible workarounds:
This issue is due to a limitation in the way Java interacts with outside interfaces such as MATLAB.
This type of problem (dynamic Java class not working with Foo Java class) results from the way that Java architects the ClassLoading mechanism. A major change was made in Java 1.2, but that change has not propagated to API's such as the one in MATLAB.
To work around this issue, you must subclass ObjectInputStream and overload the resolveClass method to check MATLAB's dynamic Class Loader. Here is a brief description from the Java documentation:
protected Class<?> resolveClass(ObjectStreamClass desc)
throws IOException,
ClassNotFoundException
Load the local class equivalent of the specified stream class description. Subclasses may implement this method to allow classes to be fetched from an alternate source.
The corresponding method in ObjectOutputStream is annotateClass. This method will be invoked only once for each unique class in the stream. This method can be implemented by subclasses to use an alternate loading mechanism but must return a Class object. Once returned, the serialVersionUID of the class is compared to the serialVersionUID of the serialized class. If there is a mismatch, the deserialization fails and an exception is raised.
By default the class name is resolved relative to the class that called readObject.