Macaronnos
Macaronnos

Reputation: 647

Matlab doesn't see changes in java class when recompiled

I work with Matlab and try to launch java class from it. So I add the path of java class to the classpath.txt in Matlab. Then I create an object of the java class HelloWorld and it works perfectly:

o = HelloWorld;
javaMethod('main', o);

However, when I change the code in Helloworld, Matlab doesn't see this: I need to restart Matlab to work with changed code of the Helloworld.

Is there a way to force Matlab to see changes in Helloworld without restarting?

Upvotes: 1

Views: 483

Answers (1)

Hoki
Hoki

Reputation: 11812

I guess you tried the Matlab clear classes without success

If I take it from Yair Altman "Undocumented Secrets of MATLAB-Java Programming" :

Provided you didn't change the java class signature, you may be able to get away with it:

Java classes are not reloaded automatically by Matlab, when recompiled outside Matlab . to reload a modified Java class, we need to restart the JVM by restarting Matlab . For classes placed on the dynamic classpath, you can try Matlab’s clear(‘java’) command, while remembering its side effect of clearing all globals . However, this does not always work (e .g ., if the class signature has changed).

Apparently, if you feel like an expert Java programmer, there are potential workarounds:

Expert Java programmers can try to use Paul Milenkovic’s suggestion for a proxy classloader, as an alternative to restarting Matlab or clearing Java . as Dan Spielman explains, “the rough idea is that you create a classloader for your class, and then access it through the classloader . after you recompile, you kill the classloader and then create a new instance of it, which then reads the recompiled class” .

But the bottom line is:

In practice, I suggest restarting Matlab after Java classes are recompiled, even when this is not strictly necessary . It may save a lot of frustrating debugging and chasing down errors that only happen because Matlabkeeps an old class in memory.

Upvotes: 2

Related Questions