donalmg
donalmg

Reputation: 655

JNI call to Java function returning an Object results in NoSuchMethodError

I have a Java function which returns a singleton instance of a Class

public static synchronized MyClass getInstance() throws MyClassException{
    if (instance == NULL){
        // create
    } 
    return instance;
}

I want to call this through C++ code, but when I do, it returns a NoSuchMethodError.

cls = jenv->FindClass("MyClass");
if (cls == NULL)
{
//error handling
}
mid = jenv->GetStaticMethodID(cls, "getInstance", "()LMyClass");
if (mid == NULL)
{
//error handling
}

When I run:

javap -s -p on MyClass, I get the signature:
public static synchronized MyClass getInstance()   throws MyClassException;
Signature: ()LMyClass; 

If I change the function signature to void in the Java class, the GetStaticMethodID call works as expected.

Do I need to setup a jobject to expect the return value from the call?

Is this possible without calling GetStaticMethodID first?

Upvotes: 5

Views: 3887

Answers (1)

eternaln00b
eternaln00b

Reputation: 1053

I believe the problem is that it's unable to resolve the output argument specified. If your java class were in the package: "com/work/", you would say:

jenv->GetStaticMethodID(cls, "getInstance", "()Lcom/work/MyClass;");

That should do it.

EDIT:

It looks like the answer is in the output of javap isn't it? You should be doing:

jenv->GetStaticMethodID(cls, "getInstance", "()LMyClass;");

Upvotes: 1

Related Questions