Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Importing user created Java Classes

Subject: Importing user created Java Classes

From: Craig

Date: 27 Nov, 2007 02:54:02

Message: 1 of 6

Hi everyone,
I've been having an ongoing problem with bringing my java
classes into MatLab.
First of all, I created my class "Test" (part of the MLT
package) in eclipse with Java 1.5.
Then I jar'd it into myTest.jar, specifying the main class
in the manifest.
I then added this myTest.jar to the dynamic classpath using
"javaaddpath('/.../myTest.jar')" When I check the classpath
I can see it listed.

At this point I should be able to use the methods of the
class right? But when I type "methodsview MLT.Test" to see
the methods I get:

>> methodsview MLT.Test
??? Error using ==> methodsview
No class MLT.Test can be located or no methods for class
MLT.Test

Similarly:

>> methodsview Test
??? Error using ==> methodsview
No class Test can be located or no methods for class Test

Anybody have ANY idea what's going on? Matlab is running
Java 1.5, I've compiled it in 1.5, I've specified the path.
Am I missing something? The MathWorks help pages seem pretty
sparse in this area...
Any help would be great,
Craig

Subject: Importing user created Java Classes

From: Bob Gilmore

Date: 27 Nov, 2007 05:27:12

Message: 2 of 6

Craig,
You've already overcome (from what I've seen) the most common problem in
using Java from within MATLAB; you've got the packaging right. That is,
you put the class within a package, then tried to access it using
packagename.classname syntax.

Here are the next things that I would try, if I were you:
(1) Try using an absolute path in your javaaddpath() call, rather than a
relative path. The Java runtime "fixes" the current working directory (and
therefore the meaning of ./, ../, etc.) when it is launched. We can't make
it "follow MATLAB's current working directory." Therefore, "../" might not
mean what you think it means. Specify the path in full and see if that
solves the problem.
(2) Does the class that you compiled depend on other classes or packages
that you HAVEN'T added to MATLAB's javaclasspath? If so, the class loader
fails. Java doesn't tell MATLAB that the class failed to load because
soemthing that it depends on failed to load; it just tells MATLAB the class
couldn't be loaded properly. THAT could be the cause of the error.
(2a) I don't know what version of Java your version of MATLAB is running,
but if there's major "version skew," MATLAB's JVM might not be able to run
your class. Run
>> version -java
and see what it tells you. If it isn't 1.5 or later, that could be a
problem.

Hope this helps,
--
Bob Gilmore, The MathWorks, Inc.

"Craig " <craig@mathworks.com> wrote in message
news:fig0sa$b3i$1@fred.mathworks.com...
> Hi everyone,
> I've been having an ongoing problem with bringing my java
> classes into MatLab.
> First of all, I created my class "Test" (part of the MLT
> package) in eclipse with Java 1.5.
> Then I jar'd it into myTest.jar, specifying the main class
> in the manifest.
> I then added this myTest.jar to the dynamic classpath using
> "javaaddpath('/.../myTest.jar')" When I check the classpath
> I can see it listed.
>
> At this point I should be able to use the methods of the
> class right? But when I type "methodsview MLT.Test" to see
> the methods I get:
>
>>> methodsview MLT.Test
> ??? Error using ==> methodsview
> No class MLT.Test can be located or no methods for class
> MLT.Test
>
> Similarly:
>
>>> methodsview Test
> ??? Error using ==> methodsview
> No class Test can be located or no methods for class Test
>
> Anybody have ANY idea what's going on? Matlab is running
> Java 1.5, I've compiled it in 1.5, I've specified the path.
> Am I missing something? The MathWorks help pages seem pretty
> sparse in this area...
> Any help would be great,
> Craig

Subject: Importing user created Java Classes

From: Yair Altman

Date: 27 Nov, 2007 06:54:49

Message: 3 of 6

"Bob Gilmore" <bgilmore@mathworks.com> wrote in message
<fig9ri$ge8$1@fred.mathworks.com>...
> Craig,
> You've already overcome (from what I've seen) the most
common problem in
> using Java from within MATLAB; you've got the packaging
right. That is,
> you put the class within a package, then tried to access
it using
> packagename.classname syntax.
>
> Here are the next things that I would try, if I were you:
> (1) Try using an absolute path in your javaaddpath() call,
rather than a
> relative path. The Java runtime "fixes" the current
working directory (and
> therefore the meaning of ./, ../, etc.) when it is
launched. We can't make
> it "follow MATLAB's current working directory."
Therefore, "../" might not
> mean what you think it means. Specify the path in full
and see if that
> solves the problem.
> (2) Does the class that you compiled depend on other
classes or packages
> that you HAVEN'T added to MATLAB's javaclasspath? If so,
the class loader
> fails. Java doesn't tell MATLAB that the class failed to
load because
> soemthing that it depends on failed to load; it just tells
MATLAB the class
> couldn't be loaded properly. THAT could be the cause of
the error.
> (2a) I don't know what version of Java your version of
MATLAB is running,
> but if there's major "version skew," MATLAB's JVM might
not be able to run
> your class. Run
> >> version -java
> and see what it tells you. If it isn't 1.5 or later, that
could be a
> problem.
>
> Hope this helps,
> --
> Bob Gilmore, The MathWorks, Inc.
>
> "Craig " <craig@mathworks.com> wrote in message
> news:fig0sa$b3i$1@fred.mathworks.com...
> > Hi everyone,
> > I've been having an ongoing problem with bringing my java
> > classes into MatLab.
> > First of all, I created my class "Test" (part of the MLT
> > package) in eclipse with Java 1.5.
> > Then I jar'd it into myTest.jar, specifying the main class
> > in the manifest.
> > I then added this myTest.jar to the dynamic classpath using
> > "javaaddpath('/.../myTest.jar')" When I check the classpath
> > I can see it listed.
> >
> > At this point I should be able to use the methods of the
> > class right? But when I type "methodsview MLT.Test" to see
> > the methods I get:
> >
> >>> methodsview MLT.Test
> > ??? Error using ==> methodsview
> > No class MLT.Test can be located or no methods for class
> > MLT.Test


You should also check that your Test class and relevant
methods were declared public.

Yair Altman
http://ymasoftware.com

Subject: Importing user created Java Classes

From: Craig

Date: 28 Nov, 2007 18:59:42

Message: 4 of 6

Thanks a lot Bob!
The problem was the path name. I'm running Matlab remotely
and I didn't upload the jar's to the server.
I can see the methods for this tester class now. Which is
step in the right direction.
Do you happen to know how well Java 1.6 is 'meshed' with
Matlab running a 1.6?
I have a GUI I'm almost done developing in NetBeans and I'd
like to be able to import it to Matlab with minimal hassels
so I can use MatLab to crunch some numbers for me.
Is this possible? Should I have designed this in Matlab from
the start? I really like the look/feel of java GUI's so I'd
prefer not to write the whole thing over again.
Your two cents worth on this situation would be VERY much
appreciated.
Thanks
Craig

"Bob Gilmore" <bgilmore@mathworks.com> wrote in message
<fig9ri$ge8$1@fred.mathworks.com>...
> Craig,
> You've already overcome (from what I've seen) the most
common problem in
> using Java from within MATLAB; you've got the packaging
right. That is,
> you put the class within a package, then tried to access
it using
> packagename.classname syntax.
>
> Here are the next things that I would try, if I were you:
> (1) Try using an absolute path in your javaaddpath() call,
rather than a
> relative path. The Java runtime "fixes" the current
working directory (and
> therefore the meaning of ./, ../, etc.) when it is
launched. We can't make
> it "follow MATLAB's current working directory."
Therefore, "../" might not
> mean what you think it means. Specify the path in full
and see if that
> solves the problem.
> (2) Does the class that you compiled depend on other
classes or packages
> that you HAVEN'T added to MATLAB's javaclasspath? If so,
the class loader
> fails. Java doesn't tell MATLAB that the class failed to
load because
> soemthing that it depends on failed to load; it just tells
MATLAB the class
> couldn't be loaded properly. THAT could be the cause of
the error.
> (2a) I don't know what version of Java your version of
MATLAB is running,
> but if there's major "version skew," MATLAB's JVM might
not be able to run
> your class. Run
> >> version -java
> and see what it tells you. If it isn't 1.5 or later, that
could be a
> problem.
>
> Hope this helps,
> --
> Bob Gilmore, The MathWorks, Inc.
>
> "Craig " <craig@mathworks.com> wrote in message
> news:fig0sa$b3i$1@fred.mathworks.com...
> > Hi everyone,
> > I've been having an ongoing problem with bringing my java
> > classes into MatLab.
> > First of all, I created my class "Test" (part of the MLT
> > package) in eclipse with Java 1.5.
> > Then I jar'd it into myTest.jar, specifying the main class
> > in the manifest.
> > I then added this myTest.jar to the dynamic classpath using
> > "javaaddpath('/.../myTest.jar')" When I check the classpath
> > I can see it listed.
> >
> > At this point I should be able to use the methods of the
> > class right? But when I type "methodsview MLT.Test" to see
> > the methods I get:
> >
> >>> methodsview MLT.Test
> > ??? Error using ==> methodsview
> > No class MLT.Test can be located or no methods for class
> > MLT.Test
> >
> > Similarly:
> >
> >>> methodsview Test
> > ??? Error using ==> methodsview
> > No class Test can be located or no methods for class Test
> >
> > Anybody have ANY idea what's going on? Matlab is running
> > Java 1.5, I've compiled it in 1.5, I've specified the path.
> > Am I missing something? The MathWorks help pages seem pretty
> > sparse in this area...
> > Any help would be great,
> > Craig
>
>

Subject: Importing user created Java Classes

From: Tarakeshwar

Date: 21 Jan, 2009 13:39:02

Message: 5 of 6

Hi Criag,
        I am facing similar issue what does it mean by uploading jar to server???

"Craig " <craig@mathworks.com> wrote in message <fikdqu$4kd$1@fred.mathworks.com>...
> Thanks a lot Bob!
> The problem was the path name. I'm running Matlab remotely
> and I didn't upload the jar's to the server.
> I can see the methods for this tester class now. Which is
> step in the right direction.
> Do you happen to know how well Java 1.6 is 'meshed' with
> Matlab running a 1.6?
> I have a GUI I'm almost done developing in NetBeans and I'd
> like to be able to import it to Matlab with minimal hassels
> so I can use MatLab to crunch some numbers for me.
> Is this possible? Should I have designed this in Matlab from
> the start? I really like the look/feel of java GUI's so I'd
> prefer not to write the whole thing over again.
> Your two cents worth on this situation would be VERY much
> appreciated.
> Thanks
> Craig
>
> "Bob Gilmore" <bgilmore@mathworks.com> wrote in message
> <fig9ri$ge8$1@fred.mathworks.com>...
> > Craig,
> > You've already overcome (from what I've seen) the most
> common problem in
> > using Java from within MATLAB; you've got the packaging
> right. That is,
> > you put the class within a package, then tried to access
> it using
> > packagename.classname syntax.
> >
> > Here are the next things that I would try, if I were you:
> > (1) Try using an absolute path in your javaaddpath() call,
> rather than a
> > relative path. The Java runtime "fixes" the current
> working directory (and
> > therefore the meaning of ./, ../, etc.) when it is
> launched. We can't make
> > it "follow MATLAB's current working directory."
> Therefore, "../" might not
> > mean what you think it means. Specify the path in full
> and see if that
> > solves the problem.
> > (2) Does the class that you compiled depend on other
> classes or packages
> > that you HAVEN'T added to MATLAB's javaclasspath? If so,
> the class loader
> > fails. Java doesn't tell MATLAB that the class failed to
> load because
> > soemthing that it depends on failed to load; it just tells
> MATLAB the class
> > couldn't be loaded properly. THAT could be the cause of
> the error.
> > (2a) I don't know what version of Java your version of
> MATLAB is running,
> > but if there's major "version skew," MATLAB's JVM might
> not be able to run
> > your class. Run
> > >> version -java
> > and see what it tells you. If it isn't 1.5 or later, that
> could be a
> > problem.
> >
> > Hope this helps,
> > --
> > Bob Gilmore, The MathWorks, Inc.
> >
> > "Craig " <craig@mathworks.com> wrote in message
> > news:fig0sa$b3i$1@fred.mathworks.com...
> > > Hi everyone,
> > > I've been having an ongoing problem with bringing my java
> > > classes into MatLab.
> > > First of all, I created my class "Test" (part of the MLT
> > > package) in eclipse with Java 1.5.
> > > Then I jar'd it into myTest.jar, specifying the main class
> > > in the manifest.
> > > I then added this myTest.jar to the dynamic classpath using
> > > "javaaddpath('/.../myTest.jar')" When I check the classpath
> > > I can see it listed.
> > >
> > > At this point I should be able to use the methods of the
> > > class right? But when I type "methodsview MLT.Test" to see
> > > the methods I get:
> > >
> > >>> methodsview MLT.Test
> > > ??? Error using ==> methodsview
> > > No class MLT.Test can be located or no methods for class
> > > MLT.Test
> > >
> > > Similarly:
> > >
> > >>> methodsview Test
> > > ??? Error using ==> methodsview
> > > No class Test can be located or no methods for class Test
> > >
> > > Anybody have ANY idea what's going on? Matlab is running
> > > Java 1.5, I've compiled it in 1.5, I've specified the path.
> > > Am I missing something? The MathWorks help pages seem pretty
> > > sparse in this area...
> > > Any help would be great,
> > > Craig
> >
> >
>

Subject: Importing user created Java Classes

From: Andrew

Date: 10 Jun, 2013 02:39:12

Message: 6 of 6

You must also ensure that you are compiling with the correct version of Java. If you are on Linux you are likely using OpenJDK, but I have found that this will not work with matlab. I also found that changing the Java version that Matlab uses is not easy (Matlab docs say to change the MATLAB_JAVA environment variable, but this does not work).

Here is a quick way to compile your java in Eclipse for use in MATLAB:
Make your project import all your source files.
Right click the project go to libraries.
Select add a new library.
Select the directory $MATLAB_HOME/sys/java/jre.
Eclipse should find that the path is the path of the JRE. Set a name for the JRE like 'matlabJRE' or something. Click Finish.
Make sure that there are no other JREs in your build path.

Hopefully at this point you have no compilation errors (AKA, your code was not dependent on JDK1.7 or something like that). As a last step, right click your project, go to export, then export your project as a jar file.

In MATLAB, run the javaaddclass() and import mypackage.myclass commands, and you should be good to go. Good luck.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us