简体   繁体   中英

Create Java object that implements an interface from Matlab

For some reason, I am not able to create a Java object that implements a user-defined interface.

I tried creating a Java object that implements a pre-defined interface and it worked fine.

My interface:

public interface Speak 
{
    public void sayHello();
}

My Class:

public class myPerson 
implements Speak
{
    public myPerson(String arg_firstName, int arg_age)
    {
        firstName = arg_firstName;
        age = arg_age;
    }

    public String firstName;
    public int age;

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
    }
}

For my class to work in eclipse, I had to export my interface as a .jar file, then I added it to the project libraries - and it worked just fine.

My Matlab file:

clc
clear

javaclasspath('/path/to/Speak.jar');
javaclasspath('/path/to/myPerson.jar');

driver_1 = myPerson('Bob', 39);

The error that I'm getting is:

Undefined function or variable 'myPerson'.

If I remove the implements interface, I can create the object just fine.

I suggest you try

javaclasspath({'/path/to/Speak.jar', '/path/to/myPerson.jar'});

(You need both Speak.jar and myPerson.jar on the classpath to instantiate a myPerson .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM