1

I'm new to JNI and have a Java program given, from which I want to call methods in C++. I have an ObjectA implemented in Java. I receive its classID like this in C++:

jclass cls = env->FindClass("myPackages/ObjectA");

Now I have the method funcA given in Java. funcA accepts an Object of the type ObjectA as an argument and returns an integer. The declaration in Java looks like this:

public int funcA( ObjectA obj);

Now I want to get the methodID of funcA in C++. The problem is, I don't know how to specify which parametertype the method gets. I know that I have to write L fully-qualified-class ; to pass Objects like a String, but how do I do this, when the objects are not from official javalibraries but objects I created? I tried this, but it obviously didn't work:

jmethodID jfuncA = env->GetMethodID(cls, "funcA", "(Lcls;)I");

All I got as a response is, that the Method was not found. So what do I have to write instead of (Lcls;)? Or is this impossible?

Any idea is useful!

1
  • That's simply not how the C++ programming language works. cls is a variable in your code. You can't use it inside a string literal! Why don't you just write myPackages/ObjectA instead? Then it would work. Commented Jun 28, 2013 at 22:39

1 Answer 1

5

Run javap -s on your compiled Java class and use exactly what it tells you as the signature of the native method. Cut and paste. Don't waste your time trying to figure it out for yourself when you have a tool that is never wrong.

Sign up to request clarification or add additional context in comments.

4 Comments

javap -s -p to include private methods.
@Jakob No, that's my point. Why do it yourself when the computer can?
@EJP True, but it helped me to understand how it works when I started working with JNI.
you have to give the full class path ...like Lcom/mypackage/subpack/Class; ...have a look at this question stackoverflow.com/questions/11803927/… .you can access even the java class members by gettings the field ids....also this might help stackoverflow.com/questions/18381076/…

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.