Got Questions? Get Answers.
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:
Trouble calling java from Matlab

Subject: Trouble calling java from Matlab

From: John

Date: 16 Nov, 2009 22:12:18

Message: 1 of 6

I am trying to use my class in matlab.

I put it on the path (or tried) these ways.

javaaddpath('/Users/jsparger/Documents/MATLAB/')
javaaddpath('/Users/jsparger/Documents/MATLAB/MySound.class')
javaaddpath('/Users/jsparger/Documents/MATLAB/SoundPlayer.jar')


but when I try

dog = MySound();
or
dog = whack.MySound();
or
dog = SoundPlayer.whack.MySound()
or
dog = javaObject('MySound') or javaOject('MySound.class')

I get a big fat error.

I can call the built in Java classes okay, but I can't figure out how to call my own.
I want to make an object of MySound.class or of SoundPlayer.whack.MySound. Anybody have any any tips for me?

Thanks, and sorry for the stupid.
--John

Subject: Trouble calling java from Matlab

From: Yair Altman

Date: 17 Nov, 2009 07:19:03

Message: 2 of 6

"John " <durchfalldurchfall@yahoo.com> wrote in message <hdsio2$l39$1@fred.mathworks.com>...
> I am trying to use my class in matlab.
>
> I put it on the path (or tried) these ways.
>
> javaaddpath('/Users/jsparger/Documents/MATLAB/')
> javaaddpath('/Users/jsparger/Documents/MATLAB/MySound.class')
> javaaddpath('/Users/jsparger/Documents/MATLAB/SoundPlayer.jar')
>
>
> but when I try
>
> dog = MySound();
> or
> dog = whack.MySound();
> or
> dog = SoundPlayer.whack.MySound()
> or
> dog = javaObject('MySound') or javaOject('MySound.class')
>
> I get a big fat error.
>
> I can call the built in Java classes okay, but I can't figure out how to call my own.
> I want to make an object of MySound.class or of SoundPlayer.whack.MySound. Anybody have any any tips for me?
>
> Thanks, and sorry for the stupid.
> --John


Did you remember to make your class public?
Did you remember to create a public no-arguments constructor?

Yair Altman
http://UndocumentedMatlab.com

Subject: Trouble calling java from Matlab

From: John

Date: 17 Nov, 2009 16:34:21

Message: 3 of 6


> Did you remember to make your class public?
> Did you remember to create a public no-arguments constructor?
>
> Yair Altman
> http://UndocumentedMatlab.com

The class is public, and I thought that a class already had a no arg constructor if you didn't specify any other constructor.

I'm sorry to ask, but I have looked at the help:
Could you just walk me through the steps of getting a custom class into matlab and calling it? I thought I added it correctly to the path, but I guess I am not calling it correctly. What is the fully qualified name if it is in a jar file? What if it is simply the class file?

Thanks
--John

Subject: Trouble calling java from Matlab

From: Yair Altman

Date: 17 Nov, 2009 17:24:04

Message: 4 of 6

"John " <durchfalldurchfall@yahoo.com> wrote in message <hdujad$clr$1@fred.mathworks.com>...
>
> > Did you remember to make your class public?
> > Did you remember to create a public no-arguments constructor?
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
>
> The class is public, and I thought that a class already had a no arg constructor if you didn't specify any other constructor.
>
> I'm sorry to ask, but I have looked at the help:
> Could you just walk me through the steps of getting a custom class into matlab and calling it? I thought I added it correctly to the path, but I guess I am not calling it correctly. What is the fully qualified name if it is in a jar file? What if it is simply the class file?
>
> Thanks
> --John

you added the javapath correctly and used the classname correctly - simply add a public constructor to your class and that should solve it - Matlab can't access a class without a public constructor.

Yair Altman
http://UndocumentedMatlab.com
 

Subject: Trouble calling java from Matlab

From: Kyle

Date: 17 Oct, 2012 16:17:21

Message: 5 of 6

I'm having the same problem with a Java function that looks like:

// ------------------------------------
package testfunction;

public class TestFunction
{
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}
// ------------------------------------


and a Matlab Function that looks like:

%%=======================
function [ ] = JavaCallTesting( )

javaaddpath({
   'C:\Users\username\Desktop\JavaFunctions\TestFunction', ...
   'C:\Users\username\Desktop\JavaFunctions\TestFunction\dist\TestFunction.jar'...
   'C:\Users\username\Desktop\JavaFunctions\TestFunction\build\classes\testfunction\TestFunction.class'...
   });

TestFunction();

% end of file
end
%% ==============================

any suggestions?

"Yair Altman" wrote in message <hdum7k$i6r$1@fred.mathworks.com>...
> "John " <durchfalldurchfall@yahoo.com> wrote in message <hdujad$clr$1@fred.mathworks.com>...
> >
> > > Did you remember to make your class public?
> > > Did you remember to create a public no-arguments constructor?
> > >
> > > Yair Altman
> > > http://UndocumentedMatlab.com
> >
> > The class is public, and I thought that a class already had a no arg constructor if you didn't specify any other constructor.
> >
> > I'm sorry to ask, but I have looked at the help:
> > Could you just walk me through the steps of getting a custom class into matlab and calling it? I thought I added it correctly to the path, but I guess I am not calling it correctly. What is the fully qualified name if it is in a jar file? What if it is simply the class file?
> >
> > Thanks
> > --John
>
> you added the javapath correctly and used the classname correctly - simply add a public constructor to your class and that should solve it - Matlab can't access a class without a public constructor.
>
> Yair Altman
> http://UndocumentedMatlab.com
>

Subject: Trouble calling java from Matlab

From: Yair Altman

Date: 17 Oct, 2012 19:05:15

Message: 6 of 6

"Kyle " <kyle.b.johnston@nasa.gov> wrote in message <k5mlmh$nqu$1@newscl01ah.mathworks.com>...
> I'm having the same problem with a Java function that looks like:
>
> // ------------------------------------
> package testfunction;
>
> public class TestFunction
> {
> public static void main(String[] args) {
> System.out.println("Hello, World");
> }
> }
> // ------------------------------------
>
>
> and a Matlab Function that looks like:
>
> %%=======================
> function [ ] = JavaCallTesting( )
>
> javaaddpath({
> 'C:\Users\username\Desktop\JavaFunctions\TestFunction', ...
> 'C:\Users\username\Desktop\JavaFunctions\TestFunction\dist\TestFunction.jar'...
> 'C:\Users\username\Desktop\JavaFunctions\TestFunction\build\classes\testfunction\TestFunction.class'...
> });
>
> TestFunction();
[snip]


Try: TestFunction.main({'arg1','arg2'})

Yair Altman
http://UndocumentedMatlab.com
Read my Matlab-Java programming book
 

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