javaaddpath not update during runtime inside a function

1 view (last 30 days)
Hi, I encountered a problem trying to load a jar inside a function.
The problem is that when the function runs, it goes into infinite loop of the following code.
Although I use javaaddpath to load the jar, but it seems not to be loaded.
However, if I run this section in command window mode, it load for once and exit the while loop.
%% ----load jar, if not loaded
while isempty(java.lang.Class.forName('ij.IJ');
disp(['loading ij... @ ',datestr(now,31)]);
javaaddpath('D:\ij.jar'));
pause(5);
end
%...
%% ---Another example is that inside a function, I collect ~100 jars to load in
% batch by:
javaaddpath(pathsToAdd,'-end');
aIJ = ij.IJ;
Error: Undefined variable "ij" or class "ij.IJ".
% Then I check the javaclasspaath and found that
% D:\GreenSoft\Fiji.app53q\jars\ij-1.53q.jar is already shown in the
% javaclasspath, but Matlab unexpectedly refuse to load it.
% Then I manually run in command window:
javaaddpath('D:\GreenSoft\Fiji.app53q\jars\ij-1.53q.jar')
% Matlab takes about 5s to load it.
% Then I run:
aIJ = ij.IJ;
% Now the IJ object is created.
% It seems that javaaddpath works well in base workspace or a function directly called from command window,
% but not work well if located deeper in function chains.
% Because I am using MATLAB_JAVA environment variable to switch R2016a java
% from 6 to java 8, I am not sure if this is the cause of the problem.

Answers (0)

Community Treasure Hunt

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

Start Hunting!