2

I want to create a Java wrapper against some third-party library with C interface. The library operates on a complex Context entity which is essentially a C++ object (C++ is used internally in that library, but API is in pure C). It would be natural to wrap this entity into a class accessible from Java. For that, a pointer to Context should be stored somewhere.

I see two options to do this:

  • to declare a new member on java side (as long, for example) and convert it to pointer type inside JNI methods implementation
  • to declare a new member in JNI header (That might be illegal if Java relies on the size of structure it gerenated for me by javah)

All the tutorials on JNI are too simple to give me a hint on how to wrap a complex entities with Java classes, any links on more verbose documentation are appreciated.

I also want to know where it is appropriate to call Context destruction function (C++ destructor inside) I don't want to use Java finalize for that as Java don't favor finalize methods and I supect there is a way to define a destruction procedure on native side.

Basilevs
  • 22,440
  • 15
  • 57
  • 102
  • 1
    possible duplicate of [What is the 'correct' way to store a native pointer inside a Java object?](http://stackoverflow.com/questions/337268/what-is-the-correct-way-to-store-a-native-pointer-inside-a-java-object) – Joachim Sauer Mar 09 '11 at 14:36
  • And finalization is described http://stackoverflow.com/questions/214699/jni-memory-management-using-the-invocation-api I can't make myself beleive there are no better methods though. – Basilevs Mar 09 '11 at 14:55

0 Answers0