5

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?

3
  • Where is the call that's returning the error? Commented Feb 1, 2012 at 20:09
  • When I run the C++ app, I get: Exception in thread "main" java.lang.NoSuchMethodError: getInstance Commented Feb 1, 2012 at 20:16
  • I wonder if you need to specify the fully qualified path to "MyClass" in the call to GetStaticMethodId. For example: jenv->GetStaticMethodID(cls, "getInstance", "()Lcom/work/MyClass;"); Depending on where the CPP code lives relative to the Java code? Just a thought. Commented Feb 1, 2012 at 20:19

1 Answer 1

1

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;");
Sign up to request clarification or add additional context in comments.

4 Comments

Typo in my original question. I had this as ()LMyClass
And you explicitly said "()LMyClass;" with the semi-colon at the end?
For example, I can do: getStaticMethodID(cls, "currentThread", "()Ljava/lang/Thread;"); And that works fine. Does that work for you?
Yep - missed the semi-colon. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.