Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI Calls different in C vs C++?

So i have the following code in C that utilizes Java Native Interface however i would like to convert this to C++ but am not sure how.

 #include <jni.h>  #include <stdio.h>  #include "InstanceMethodCall.h"   JNIEXPORT void JNICALL   Java_InstanceMethodCall_nativeMethod(JNIEnv *env, jobject obj)  {      jclass cls = (*env)->GetObjectClass(env, obj);      jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "()V");      if (mid == NULL) {          return; /* method not found */      }      printf("In C\n");      (*env)->CallVoidMethod(env, obj, mid);  } 

Java Program:

 class InstanceMethodCall {      private native void nativeMethod();      private void callback() {          System.out.println("In Java");      }      public static void main(String args[]) {          InstanceMethodCall c = new InstanceMethodCall();          c.nativeMethod();      }      static {          System.loadLibrary("InstanceMethodCall");      }  } 

What are the differences in which JNI interacts with C and C++? Any help is greatly appreciated.

Thanks, Pete

like image 363
Petey B Avatar asked Jun 01 '09 15:06

Petey B


People also ask

What is a JNI call?

JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).

Are JNI calls expensive?

Function calls to JNI methods are expensive, especially when calling a method repeatedly. Native methods are not inlined by the JVM, nor can the method be JIT compiled, as the method is already compiled.

What is JNI ENV?

The JNIEnv type is a pointer to a structure storing all JNI function pointers. It is defined as follows: typedef const struct JNINativeInterface *JNIEnv; The VM initializes the function table, as shown by Code Example 4-1. Note that the first three entries are reserved for future compatibility with COM.


1 Answers

I used to have the book Essential JNI. And while it is kinda dated, much of it still works today.

If I recall correctly, in C, Java constructs are simply pointers. Thus, in your code, "(*env)->" is dereferencing pointers to give you access to the underlying methods.

For C++, "env" is actually an object - a different entity than a C pointer. (And JNI can actually provide real objects for your C++ code to manipulate, since C++ actually supports objects.) So "env->" has a different meaning in C++, it means "call the method that is contained in the object pointed to by "env".

The other difference, I believe, is that many of the C-JNI functions require that one of your parameters be the "JNIEnv *env". So in C you might have to say (*env)->foo(env, bar). With c++, the second reference to "env" is not necessary, so you can instead say "env->foo(bar)"

Unfortunately, I don't have the above book in front of me, so I can't quite confirm this! But I think investigating those two things (specifically looking for them on google or in other JNI code) will get you pretty far.

like image 130
poundifdef Avatar answered Oct 08 '22 22:10

poundifdef