How come javaaddpath + import do not work in a script/function?

6 views (last 30 days)
Hi,
I am trying to import several classes in a third-party jar package. If I run the following code in the command line, the classes can be normally imported:
javaaddpath([pwd filesep 'jbox2d_lib.jar']);
import org.jbox2d.collision.shapes.PolygonShape;
However, when I put the above code in a script or function, then MATLAB reports the following error:
Error: File: DerpHerp.m Line: 2 Column: 8
Arguments to IMPORT must either end with "."*
or else specify a fully qualified class name:
"org.jbox2d.collision.shapes.PolygonShape" fails this test
In the following link, a similar problem was described:
The only exception is that in my case, rerunning the script/function does NOT solve the problem. And the link said the 'bug' was fixed in R2010b, but I tried the same program on R2011a 32-bit, R2012a (32-bit + 64-bit), R2012b 32-bit, all reported exactly the same problem.
Someone says that the package will not load correctly if it depends on classes found in other packages. I don't think this is my case since this package can be correctly loaded after all, only in a slightly awkward way.
Adding 'clear java' after javaaddpath does not help.
MORE DETAILS:
-------------
1. When this error occurs, the folder [pwd filesep 'jbox2d_lib.jar'] is not added to the java DYNAMIC path at all, although javaaddpath does not generate any error, and this jar file definitely exists in the current directory.
2. If I put javaaddpath in script1.m and put the import in script2.m, and manually run both of them, it works fine: %SCRIPT1.m
javaaddpath([pwd filesep 'jbox2d_lib.jar']);
% SCRIPT2.m
import org.jbox2d.collision.shapes.PolygonShape;
>> script1.m % After this, the path is successfully added >> script2.m % the classes are loaded normally
However, if script2.m calls script1.m before import, then the problem would appear
%SCRIPT1.m
javaaddpath([pwd filesep 'jbox2d_lib.jar']);
% SCRIPT2.m
script1;
import org.jbox2d.collision.shapes.PolygonShape;
>> script2.m % ERROR! and folder is not added after running this
---------------
So in conclusion, it seems that I just can't do javaaddpath and import in one go. I have to manually run two different scripts to get them to work properly. Anyone know what might be the cause and how to solve it? Thanks in advance!
  3 Comments
Sven
Sven on 10 Apr 2014
Edited: Sven on 10 Apr 2014
I have also noticed this kind of behaviour and summarise it as: "MATLAB cannot add a Java class to its path and use that class within the same execution."
Weird behaviour I've had include simply not recognising the class that I had just added, or under some circumstances recognising that class once (the first time it was added to the path) but then having it removed from the java classpath after execution of the current function/statement such that I could not use that java object in any persistent variables.
My solution has been to add any potential Java classes to my path within my startup script. This of course may not be sufficient in some situations but it's a work-around.
Chris Anderson
Chris Anderson on 3 Feb 2017
I also noticed this exact behavior and found that Ronald's solution of wrapping the import commands with eval() worked. Thank you, Ronald!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!