Sina Iravanian
Sina Iravanian

Reputation: 16286

Matlab locks the jar file when I try to call Java methods from it

I'm developing a library in Java and need to use it in Matlab. Both Java and Matlab codes are under constant development; therefore whenever I rebuild the Java code and create a new jar file, I need to reload it in the Matlab code and use the fresh jar file there. My problem is that Matlab locks the jar file and I cannot replace the old jar file with the new one, unless I restart the whole Matlab environment. How can I make Matlab unlock my jar file?

I access the Java classes and methods through

javaaddpath('path/to/jar/file.jar');

Also in the end I call:

javarmpath('path/to/jar/file.jar');

but it does not unlock the file either. I use Matlab 7.9.0 (R2009b).

Upvotes: 2

Views: 570

Answers (2)

Thierry Dalon
Thierry Dalon

Reputation: 926

You can load the .jar files dynamically without being locked using ClassPathHacker as explained in https://stackoverflow.com/a/4380605/2043349.

You can download a jar package from https://code.google.com/p/ratrix/source/browse/classes/db/cpath/ClassPathHacker.java?r=8ee84cd195104df02b5e8530190590aff465d914.

Then load the file using following code:

cpathPath=[hpath,'\Utilities\java\cpath.jar'];
javaaddpath(cpathPath)
import cpath.*
cpath.ClassPathHacker.addFile(jarfile)

(Adapt hpath and jarfile to your need.)

Upvotes: 0

Stephen C
Stephen C

Reputation: 718826

I suspect that you can't. Underneath the hood MatLab contains a JVM, and it is the JVM that is locking the JAR file. It is done to prevent nasty things happening to the JVM when something outside modifies the JAR file under its feet.

Now if this was your own application, you could write a custom classloader that worked around this problem. But I doubt that MatLab will let you do that.

Upvotes: 1

Related Questions