Reputation: 259
I'm trying the following code to call a Java function performSHA() from C code. But I keep getting an error saying "request for member ‘DestroyJavaVM’ in something not a structure or union". I've checked several links online and seems like I'm using it as mentioned.
JavaVM *jvm;
JNIEnv *env;
JavaVMOption options[3];
JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_2;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
options[0].optionString = "/home/amy/jni/";
vm_args.options = options;
JNI_CreateJavaVM(&jvm, &env, &vm_args);
jclass cls = (*env)->FindClass(env, "CallToBCLib");
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "performSHA", "ILjava/lang/String;");
(*env)->CallStaticVoidMethod(cls, mid, algo_id, tc->m1);
jvm->DestroyJavaVM(); <-- ERROR
Also is the usage of FindClass, GetStaticMethodID and CallStaticVoidMethod correct? performSHA is the Java function I'm calling and algo_id, tc->m1 are my args to the Java function...
Upvotes: 0
Views: 518
Reputation: 651
This is a bit too long to be in a comment so I'll put it as an answer.
I checked how you should access DestroyJavaVM
in the jni source code:
The function is defined here and this is the declaration:
jint JNICALL jni_DestroyJavaVM(JavaVM *vm)
At the bottom of that page is a function invocation table for usage in C source code.
The specification gives us the definition of the JavaVM type here.
We can see that the definition of the JavaVM type is the function invocation table itself.
This means we need to take our jvm
variable and access its DestroyJavaVM
function,
while also passing the jvm instance itself, so the accessing code should look like this:
jvm->DestroyJavaVM(jvm);
I haven't tested this and it's a bit weird to call the function like that but I'm guessing this should work.
Upvotes: 0
Reputation: 24294
According to the documentation, you should provide a parameter of type JavaVM *vm
to the DestroyJavaVM
function:
jint DestroyJavaVM(JavaVM *vm);
Therefore, replace jvm->DestroyJavaVM()
with DestroyJavaVM(jvm)
.
Note that on the same page, there is an "Overview" with example where DestroyJavaVM()
is called as in your code (jvm->DestroyJavaVM()
) but this applies to C++ and you mentioned you are trying to call Java code from C.
Upvotes: 0