Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 274
donalmg Avatar asked Nov 14 '22 10:11

donalmg


1 Answers

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;");
like image 52
eternaln00b Avatar answered Nov 16 '22 03:11

eternaln00b