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!

user2479025
  • 11
  • 1
  • 4
  • 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. – main-- Jun 28 '13 at 22:39

1 Answers1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 3
    `javap -s -p` to include private methods. – Tom Blodget Jun 28 '13 at 13:49
  • @Jakob No, that's my point. Why do it yourself when the computer can? – user207421 Oct 01 '13 at 21:53
  • @EJP True, but it helped me to understand how it works when I started working with JNI. – Jakob Oct 02 '13 at 07:35
  • you have to give the full class path ...like Lcom/mypackage/subpack/Class; ...have a look at this question http://stackoverflow.com/questions/11803927/accessing-a-java-object-in-a-java-object-in-c-using-jni .you can access even the java class members by gettings the field ids....also this might help http://stackoverflow.com/questions/18381076/how-to-pass-and-receive-objects-using-jni – Shrish May 18 '15 at 10:05