Time for action – calling back Java from native code
Let's continue our Store
by calling back the interface we defined from native code:
- In
com_packtpub_store_Store.cpp
, declare method descriptors with typejmethodID
for each callback, which is going to be cached:... static Store gStore; static jclass StringClass; static jclass ColorClass; static jmethodID MethodOnSuccessInt; static jmethodID MethodOnSuccessString; static jmethodID MethodOnSuccessColor; ...
- Then, cache all the callback descriptors in
JNI_OnLoad()
. This can be done in two main steps:Getting a Class descriptor with the JNI method
FindClass()
. One can find a class descriptor, thanks to its absolute package path, here:com./packtpub/store/Store
.Retrieving a method descriptor from the class descriptor with
GetMethodID()
. To differentiate several overloaded methods, the signatures retrieved earlier withjavap
must be specified:... JNIEXPORT jint JNI_OnLoad(JavaVM* pVM, void* reserved) { JNIEnv *env; if (pVM...