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?
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;");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With