MATLAB retaining control of JAR file

12 views (last 30 days)
Paul
Paul on 5 Nov 2014
Commented: Malcolm Lidierth on 19 May 2016
Hi,
I'm working with java classes and cannot seem to get Matlab to release the JAR files. The only option is to re-start Matlab.
-------------------------------
Simple sample code (java):
package basicFunctions;
public class mathFunctions {
public static int getSqr(int dataInput) {
return dataInput*dataInput;
}
}
Corresponding Matlab code:
basicFunctions.mathFunctions.getSqr(2)
Result:
4
-------------------------------
Okay great, it works. What if I re-run with an input of 3?
9
Things seem to be working as expected. But now if I try to delete my JAR file to use an updated version:
This action can't be completed because the file is open in MATLAB(R2014b)
I get this error regardless of whether I used the static or dynamic java class path. In the dynamic case, I did remove the paths after use via:
javaP = javaclasspath('-dynamic');
javarmpath(javaP{:});
-------------------------------
Other variations on this question
MathWorks Support Team says:
The ability to explicity unload Java classes is not available in MATLAB.
MATLAB uses Java's class-loader features to the extent possible to reload new class definitions,
while the Java Virtual Machine (JVM) controls when classes get unloaded, and hence whether or not JAR
files are released.
If the objects associated with the JAR files are no longer in use, see the Related Solution listed
below about forcing Java garbage collection.
No solution
-------------------------------
Tried solutions
1) Force java garbage collection
I tried both
java.lang.System.gc()
java.lang.Runtime.getRuntime().gc
No luck.
2) Variations on clear
clear all
clear java
clear import
clear classes
3) fOpen and fClose after use
With fOpen, fid = 3 -> success
With fClose, result is 0 -> success
-------------------------------
Other (Potentially) Relevant Details
Yes my compiler is set to the same version as Matlab
  2 Comments
Aaron  Wetzler
Aaron Wetzler on 11 May 2016
Edited: Aaron Wetzler on 11 May 2016
This worked for me:
javarmpath ./
javarmpath ./myfile.jar
close all
clear all
fclose all
java.lang.System.gc()
java.lang.Runtime.getRuntime().gc
javaaddpath ./
javaaddpath ./myfile.jar
Malcolm Lidierth
Malcolm Lidierth on 19 May 2016
If a class reference is still active in the JVM - or has leaked - it will not generally be possible to reload a jar using that class.
Situations that might give rise to a leak include e.g. deleting a variable containing a reference while viewing it in the MATLAB variable inspector.
As an 'orphan' reference may then exist, garbage collection will not work. The reference is reachable within the JVM in principle - so not eligible for collection - but not necassarily by any mechanism available via MATLAB.
Other MATLAB internals might also maintain a reference which I suspect is why TMW say "The ability to explicity unload Java classes is not available in MATLAB."

Sign in to comment.

Answers (0)

Categories

Find more on Java Package Integration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!