MATLAB retaining control of JAR file

조회 수: 9 (최근 30일)
Paul
Paul 2014년 11월 5일
댓글: Malcolm Lidierth 2016년 5월 19일
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
Aaron  Wetzler
Aaron Wetzler 2016년 5월 11일
편집: Aaron Wetzler 2016년 5월 11일
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 2016년 5월 19일
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."

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by