f



Calling Java from Matlab

I am unable to load user defined java classes into Matlab. java...
classes load fine. I have updated classpath.txt in the starting
directory to include the package parent directory.

IMPORT wrt single class responds with:
Arguments to IMPORT must either end with '.*' or else specify
a fully qualified class name.

Have tried compiling java with version 1.3 compatible which I believe
may be required by Matlab.

Software in use:
Matlab 6 R12
Sun JDK 1.4.1_02 Community

Any Ideas? experience?
0
5/8/2004 4:59:49 PM
comp.soft-sys.matlab 211265 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

8 Replies
620 Views

Similar Articles

[PageSpeed] 18

"John Edwards" <Hippies8MyStash@aol.com> wrote in message
news:eedcad6.-1@webx.raydaftYaTP...
> I am unable to load user defined java classes into Matlab. java...
> classes load fine. I have updated classpath.txt in the starting
> directory to include the package parent directory.
>
> IMPORT wrt single class responds with:
> Arguments to IMPORT must either end with '.*' or else specify
> a fully qualified class name.
>
> Have tried compiling java with version 1.3 compatible which I believe
> may be required by Matlab.
>
> Software in use:
> Matlab 6 R12
> Sun JDK 1.4.1_02 Community
>
> Any Ideas? experience?

Exactly as the error message indicates: [just like in java] your import
statements must end in a .* to indicate (import all the classes in the
specified package) or a specific FULLY QUALIFIED class name, which means you
must include the package.

e.g., if I have a class 'Sun' and a class 'Moon', and they are both in the
package 'mearns.brian.examples', then I can import both with the statement:

>> import('mearns.brian.examples.*');

or I can import just the 'Sun' class with

>> import('mearns.brian.examples.Sun');

however the following WILL NOT WORK (it is not a fully qualified class name)

>> import ('Sun');

NOR will the following work

>> import('mearns.brian.examples');

in the latter example, the import command expects 'examples' to be a class
in 'mearns.brian', because it is not followed by '.*', but it is not a
class, it is a package, so the import will not work.

Does that answer you question? Let me know.

-Brian


0
bmearns1 (115)
5/10/2004 1:18:03 PM
Brian Mearns wrote:
>
>
>
> "John Edwards" <Hippies8MyStash@aol.com> wrote in message
> news:eedcad6.-1@webx.raydaftYaTP...
>> I am unable to load user defined java classes into Matlab.
> java...
>> classes load fine. I have updated classpath.txt in the starting
>> directory to include the package parent directory.
>>
>> IMPORT wrt single class responds with:
>> Arguments to IMPORT must either end with '.*' or else specify
>> a fully qualified class name.
>>
>> Have tried compiling java with version 1.3 compatible which I
> believe
>> may be required by Matlab.
>>
>> Software in use:
>> Matlab 6 R12
>> Sun JDK 1.4.1_02 Community
>>
>> Any Ideas? experience?
>
> Exactly as the error message indicates: [just like in java] your
> import
> statements must end in a .* to indicate (import all the classes in
> the
> specified package) or a specific FULLY QUALIFIED class name, which
> means you
> must include the package.
>
> e.g., if I have a class 'Sun' and a class 'Moon', and they are both
> in the
> package 'mearns.brian.examples', then I can import both with the
> statement:
>
>>> import('mearns.brian.examples.*');
>
> or I can import just the 'Sun' class with
>
>>> import('mearns.brian.examples.Sun');
>
> however the following WILL NOT WORK (it is not a fully qualified
> class name)
>
>>> import ('Sun');
>
> NOR will the following work
>
>>> import('mearns.brian.examples');
>
> in the latter example, the import command expects 'examples' to be
> a class
> in 'mearns.brian', because it is not followed by '.*', but it is
> not a
> class, it is a package, so the import will not work.
>
> Does that answer you question? Let me know.
>
> -Brian
>
>
>

Brian, more difficult I`m affraid :(
Packages and classes I understand fine and for inbuilt classes usage
is OK but it behaves as if MatLab cannot find the my user defined
class files in question or that they are not recognised as java
classes.
It may just be that I am overlooking something REALY simple?
I have updated the 'classpath.txt' file to point to my package`s
parent dirtectory but I`m not entirely sure on how the directory
structure must be specified (ie / or \ or // or \\ ) and how the
drive is identified (ie d:\ or D://.. ) I cannot find examples
anywhere to resolve the issues, ps it is a windows machine we are
talking about.
I have also tried compiling the java class`s as 1.3 compatable as I
read somwhere may be required by MatLab but alas no joy.

Has this ever been done? only I cannot find examples anywhere in the
docs or codes?

Jed

and thank you.
0
5/10/2004 1:41:11 PM
"Jed" <Hippies8MyStash@aol.com> wrote in message
news:eedcad6.1@webx.raydaftYaTP...
> Brian Mearns wrote:
> >
> >
> >
> > "John Edwards" <Hippies8MyStash@aol.com> wrote in message
> > news:eedcad6.-1@webx.raydaftYaTP...
> >> I am unable to load user defined java classes into Matlab.
> > java...
> >> classes load fine. I have updated classpath.txt in the starting
> >> directory to include the package parent directory.
> >>
> >> IMPORT wrt single class responds with:
> >> Arguments to IMPORT must either end with '.*' or else specify
> >> a fully qualified class name.
> >>
> >> Have tried compiling java with version 1.3 compatible which I
> > believe
> >> may be required by Matlab.
> >>
> >> Software in use:
> >> Matlab 6 R12
> >> Sun JDK 1.4.1_02 Community
> >>
> >> Any Ideas? experience?
> >
> > Exactly as the error message indicates: [just like in java] your
> > import
> > statements must end in a .* to indicate (import all the classes in
> > the
> > specified package) or a specific FULLY QUALIFIED class name, which
> > means you
> > must include the package.
> >
> > e.g., if I have a class 'Sun' and a class 'Moon', and they are both
> > in the
> > package 'mearns.brian.examples', then I can import both with the
> > statement:
> >
> >>> import('mearns.brian.examples.*');
> >
> > or I can import just the 'Sun' class with
> >
> >>> import('mearns.brian.examples.Sun');
> >
> > however the following WILL NOT WORK (it is not a fully qualified
> > class name)
> >
> >>> import ('Sun');
> >
> > NOR will the following work
> >
> >>> import('mearns.brian.examples');
> >
> > in the latter example, the import command expects 'examples' to be
> > a class
> > in 'mearns.brian', because it is not followed by '.*', but it is
> > not a
> > class, it is a package, so the import will not work.
> >
> > Does that answer you question? Let me know.
> >
> > -Brian
> >
> >
> >
>
> Brian, more difficult I`m affraid :(
> Packages and classes I understand fine and for inbuilt classes usage
> is OK but it behaves as if MatLab cannot find the my user defined
> class files in question or that they are not recognised as java
> classes.
> It may just be that I am overlooking something REALY simple?
> I have updated the 'classpath.txt' file to point to my package`s
> parent dirtectory but I`m not entirely sure on how the directory
> structure must be specified (ie / or \ or // or \\ ) and how the
> drive is identified (ie d:\ or D://.. ) I cannot find examples
> anywhere to resolve the issues, ps it is a windows machine we are
> talking about.
> I have also tried compiling the java class`s as 1.3 compatable as I
> read somwhere may be required by MatLab but alas no joy.
>
> Has this ever been done? only I cannot find examples anywhere in the
> docs or codes?
>
> Jed
>
> and thank you.

It *shouldn't* matter whether you use \, / \\, or //. Unfortunately, I no
longer had R13 installed so I can't actually verify that.
In case it is something really obvious you're missing, just go over this
brief check list;
    - you're pointing classpath to the .class files, not the source (.java)
files
    - Depending on your version (of ML), you may need to restart ML after
editing your classpath, and also after making changes to the class files.
    - Make sure you aren't including the "no Java" flag on your matlab.exe
call
    - Do your class names or package names conflict with existing
classes/packages in the classpath?
    - classpath needs to point to the folder above the beginning of your
package e.g.,
            for my class mearns.brian.Sun at
D:\work\java\class\mearns\brian\Sun.class,  the class path entry needs to be
D:\work\java\class NOT D:\work\java\class\mearns

If none of those help, you might want to consider contacting technical
support. I'm afraid that's about the best I can offer you, but hopefully it
will be enough.
Good luck

-Brian


0
bmearns1 (115)
5/10/2004 7:34:54 PM
Brian Mearns wrote:
>
>
>
> "Jed" <Hippies8MyStash@aol.com> wrote in message
> news:eedcad6.1@webx.raydaftYaTP...
>> Brian Mearns wrote:
>> >
>> >
>> >
>> > "John Edwards" <Hippies8MyStash@aol.com> wrote in
message
>> > news:eedcad6.-1@webx.raydaftYaTP...
>> >> I am unable to load user defined java classes into
Matlab.
>> > java...
>> >> classes load fine. I have updated classpath.txt in the
> starting
>> >> directory to include the package parent directory.
>> >>
>> >> IMPORT wrt single class responds with:
>> >> Arguments to IMPORT must either end with '.*' or else
specify
>> >> a fully qualified class name.
>> >>
>> >> Have tried compiling java with version 1.3 compatible
which I
>> > believe
>> >> may be required by Matlab.
>> >>
>> >> Software in use:
>> >> Matlab 6 R12
>> >> Sun JDK 1.4.1_02 Community
>> >>
>> >> Any Ideas? experience?
>> >
>> > Exactly as the error message indicates: [just like in
java]
> your
>> > import
>> > statements must end in a .* to indicate (import all the
classes
> in
>> > the
>> > specified package) or a specific FULLY QUALIFIED class
name,
> which
>> > means you
>> > must include the package.
>> >
>> > e.g., if I have a class 'Sun' and a class 'Moon', and they
are
> both
>> > in the
>> > package 'mearns.brian.examples', then I can import both
with
> the
>> > statement:
>> >
>> >>> import('mearns.brian.examples.*');
>> >
>> > or I can import just the 'Sun' class with
>> >
>> >>> import('mearns.brian.examples.Sun');
>> >
>> > however the following WILL NOT WORK (it is not a fully
> qualified
>> > class name)
>> >
>> >>> import ('Sun');
>> >
>> > NOR will the following work
>> >
>> >>> import('mearns.brian.examples');
>> >
>> > in the latter example, the import command expects
'examples' to
> be
>> > a class
>> > in 'mearns.brian', because it is not followed by '.*', but
it
> is
>> > not a
>> > class, it is a package, so the import will not work.
>> >
>> > Does that answer you question? Let me know.
>> >
>> > -Brian
>> >
>> >
>> >
>>
>> Brian, more difficult I`m affraid :(
>> Packages and classes I understand fine and for inbuilt classes
> usage
>> is OK but it behaves as if MatLab cannot find the my user
defined
>> class files in question or that they are not recognised as java
>> classes.
>> It may just be that I am overlooking something REALY simple?
>> I have updated the 'classpath.txt' file to point to my
package`s
>> parent dirtectory but I`m not entirely sure on how the
directory
>> structure must be specified (ie / or \ or // or \\ ) and how
the
>> drive is identified (ie d:\ or D://.. ) I cannot find examples
>> anywhere to resolve the issues, ps it is a windows machine we
are
>> talking about.
>> I have also tried compiling the java class`s as 1.3 compatable
as
> I
>> read somwhere may be required by MatLab but alas no joy.
>>
>> Has this ever been done? only I cannot find examples anywhere
in
> the
>> docs or codes?
>>
>> Jed
>>
>> and thank you.
>
> It *shouldn't* matter whether you use \, / \\, or //.
> Unfortunately, I no
> longer had R13 installed so I can't actually verify that.
> In case it is something really obvious you're missing, just go over
> this
> brief check list;
> - you're pointing classpath to the .class files, not the source
> (.java)
> files
> - Depending on your version (of ML), you may need to restart ML
> after
> editing your classpath, and also after making changes to the class
> files.
> - Make sure you aren't including the "no Java" flag on your
> matlab.exe
> call
> - Do your class names or package names conflict with existing
> classes/packages in the classpath?
> - classpath needs to point to the folder above the beginning of
> your
> package e.g.,
> for my class mearns.brian.Sun at
> D:\work\java\class\mearns\brian\Sun.class, the class path entry
> needs to be
> D:\work\java\class NOT D:\work\java\class\mearns
>
> If none of those help, you might want to consider contacting
> technical
> support. I'm afraid that's about the best I can offer you, but
> hopefully it
> will be enough.
> Good luck
>
> -Brian
>
>
>

All looks fine :( was really hoping for something cause I am
currently constructing ML objects and I dont like it much!
Many thanks for the assistance, you will be remembered if I ever
complete the project at hand.

Jed
0
5/10/2004 10:29:22 PM
Hello,

has anyone found a solution to the decribed problem? Is it a matlab
bug? 

How do you work around this?

Thanks for your answers!

King


On Sat, 8 May 2004 12:59:49 -0400, John Edwards wrote:
>I am unable to load user defined java classes into Matlab. java...
>classes load fine. I have updated classpath.txt in the starting
>directory to include the package parent directory.
>
>IMPORT wrt single class responds with:
>Arguments to IMPORT must either end with '.*' or else specify
>a fully qualified class name.
>
>Have tried compiling java with version 1.3 compatible which I believe
>may be required by Matlab.
>
>Software in use:
>Matlab 6 R12
>Sun JDK 1.4.1_02 Community
>
>Any Ideas? experience?

0
6/4/2004 12:58:37 PM
I do this all the time and it works fine and easy for me. Here are
several things I have done wrong in the past.

Do the files run correctly from java? compiling does not imply
instantiatability.

Have you restarted MATLAB since changing classpath.txt?

Is the directory you are adding to classpath.txt the one that
contains the first element of you package? Do you REALLY understand
classpath? The MATLAB classpath rules are exactly the Java classpath
rules and you have to get them exactly right.

Seeing a class is different from being able to instantiate an object.
Can you do 'methodsview' on the class name?

Make a trivial static method that returns 7. If you can call it but
not instantiate, that points to a problem.

Are you putting your .class files in a jar file? get things working
without this step first.

I use lines like this in my classpath.txt:
c:\work\joe

in c:\work\joe is a directory called foo
in foo is a directory called bar
in bar is a file named MyClass.class.
the package declaration in MyClass.java is:
package foo.bar;
Does that match yours?

I think I've used every sun compiler version and they all work fine.

It works easily and robustly once you get it exactly right.

In my experience path management is the single most common difficulty
with Java.
0
joe933 (1)
6/6/2004 10:52:12 PM
In article <eedcad6.5@webx.raydaftYaTP>, joe <joe@mathworks.com> wrote:

> I do this all the time and it works fine and easy for me. Here are
> several things I have done wrong in the past.
> 
> Do the files run correctly from java? compiling does not imply
> instantiatability.

Joe,
Your full document is an excellent summary of ways things can go wrong!  
Here are two more problems that I've seen...

(1) Are you using a class that isn't available in the version of Java 
that your MATLAB is using?  Example: if you're using MATLAB 7.0 for 
Windows (which uses Java 1.4.2), and you use a class whose static 
initialization loads a clsass that isn't available until Java 1.5, an 
error will occur.

Unfortunately, the Java runtime is vague when telling why a class 
couldn't be loaded (did it fail because the CLASS can't be loaded, or 
because one of the classes IT needs couldn't be loaded?)  Therefore, 
MATLAB can't tell you, either; it just prints an error showing that the 
class can't be loaded.

So when MATLAB tells you that it can't load a class, make sure the it 
isn't trying to do something "newer" then the VM allows.  Type
>> version -java 
to figure out which version of Java is available.

(2) Since MATLAB variables aren't "strongly typed," a function which 
usually returns one type (say, a char array, which maps to a 
java.lang.String) can sometimes return something else (like a 0x0 
double, []).  Trying to pass that to a Java method could cause an error 
such as...
??? No constructor <something> with matching signature found.

Try setting a breakpoint on the offending line of MATLAB code to make 
sure that the types of the variables that you're passing meet 
expectations.

Hope that helps,
--
Bob Gilmore, Software Engineer,
The MathWorks, Inc.
0
bgilmore (160)
6/7/2004 5:12:09 AM
Jed,
to cut a long story short: All the issues the good fellows of CSSM
have pointed out are significant and are really the kind of things
that usually go wrong when working with Java within ML.
However, the syntax of classpath I will disagree on:
on my XP machine running ML 6.5.1 the only way I could have the
classpath see my packages was using relative paths (don't ask me why,
I'm fine with it as long as it works)
for example:
/Documents and
Settings/master/Desktop/Projects/someproject/somepackage.jar

note that I do not include the drive letter.

I'm pretty confident this will work on your case also.
let us know..
Miltiades
0
salvanos1 (26)
6/7/2004 7:33:43 AM
Reply: