Matlab compiler 2012b: why is imported java class name not recognized?

1 vue (au cours des 30 derniers jours)
Sergey Fein
Sergey Fein le 10 Juin 2013
I'm using r2012b with MATLAB compiler version 4.18.
Running the following script...
import java.util.HashMap
a1 = java.util.HashMap()
a2 = HashMap()
from the MATLAB environment produces expected results, i.e.:
a1 =
{}
a2 =
{}
However, if I compile the same code and attempt to run it, I get the following error:
a1 =
{}
Undefined function or variable 'HashMap'.
Error in test1 (line 4)
Looks like we have to specify full class names for the compiled code to work, which is a problem as we can't simply compile our existing working MATLAB code without modifying it first.
Note that I'm seeing the same with other java build-in classes (not only HashMap) as well as all other (user-defined) classes I've tried. Calling a static java method gives a similar error in the compiled code only, i.e.:
Undefined variable "Collections" or class "Collections.max".
Error in test1 (line 11)
  2 commentaires
Walter Roberson
Walter Roberson le 10 Juin 2013
What I would check is what path is used (interactively) to resolve java "import", and then check in the compiled executable what the path is there and whether the jar is in one of the directories it lists. Are you adding the .jar to the deployment somehow?
Sergey Fein
Sergey Fein le 12 Juin 2013
Java paths are setup correctly. This is evident from the fact that the statement "a1 = java.util.HashMap()" works fine.

Connectez-vous pour commenter.

Réponse acceptée

Sergey Fein
Sergey Fein le 13 Juin 2013
I got a response from MathWorks support. Basically, importing java classes and using class names is not supported in scripts (but is supported in functions). There are 2 solutions:
1. Use full class names
2. Put your code into a separate matlab function, i.e.:
%%%BEGIN CODE%%%
function x = foo()
import java.lang.Math;
x = Math.round(10.12);
%%%END CODE%%%

Plus de réponses (0)

Catégories

En savoir plus sur Java Package Integration dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by