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
The main difference between JNI calls in C and CPP is this:
C-style JNI looks like (*env)->SomeJNICall(env, param1 ...)
C++ style JNI looks like env->SomeJNICall(param1 ...)
so to convert it to CPP you need to do
Java_InstanceMethodCall_nativeMethod(JNIEnv *env, jobject obj)
{
jclass cls = env->GetObjectClass(obj);
jmethodID mid = env->GetMethodID(cls, "callback", "()V");
if (mid == NULL) {
return; /* method not found */
}
printf("In C++\n");
env->CallVoidMethod(obj, mid);
//rest of your code
Also, make sure that your JNI functions follow the naming convention.
Example:
JNIEXPORT jint JNICALL Java_com_shark_JNITestLib_JNITestLib_startServer(JNIEnv* env, jobject o, jstring inputName, jstring streamName, jstring description) {
You can see that the convention is Java_(package name) _ (classname) _ (methodname)
since the one above was used in a class like
package com.shark.JNITestLib
import java.util.stuff;
public class JNITestLib
{
static
{
System.loadLibrary("myJNIlib");
}
public native synchronized int startServer(String inputName, String streamName, String description);
//more class stuff...
}
When working with JNI i made it a convention to name the class containing JNI calls to be the same name as the package. Thats why you see JNITestLib twice (and thats why my JNI works right off the bat because I always forget how to properly name the JNI calls)
Cheers, hope I helped :)