Calling static java methods from JNI
Hi
I'm trying to call a java method from a c++ code (using JNI)
This is called as a callback (java calls native code, and then the native code should call a java callback again)
when the callback is declared as non-static, this works fine:
Callback.java:
class Callbacks{
privatenativevoid nativeMethod(int depth);
privatevoid callback(int depth){
if (depth < 5){
System.out.println("In Java, depth = " + depth +", about to enter C");
nativeMethod(depth + 1);
System.out.println("In Java, depth = " + depth +", back from C");
}else
System.out.println("In Java, depth = " + depth +", limit exceeded");
}
publicvoid callme(){
nativeMethod(0);
}
}
callback.cpp:
JNIEXPORTvoid JNICALL
Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth)
{
jclass cls = env->GetObjectClass(obj);
jmethodID mid = env->GetMethodID(cls,"callback","(I)V");
if (mid == 0)
return;
printf("In C, depth = %d, about to enter Java\n", depth);
env->CallVoidMethod(obj, mid, depth);
printf("In C, depth = %d, back from Java\n", depth);
}
but when it is static, it does not work
(I get NoSuchMethodError exception):
java:
class Callbacks{
privatestaticnativevoid nativeMethod(int depth);
privatestaticvoid callback(int depth){
if (depth < 5){
System.out.println("In Java, depth = " + depth +", about to enter C");
nativeMethod(depth + 1);
System.out.println("In Java, depth = " + depth +", back from C");
}else
System.out.println("In Java, depth = " + depth +", limit exceeded");
}
publicvoid callme(){
nativeMethod(0);
}
}
cpp:
JNIEXPORTvoid JNICALL
Java_Callbacks_nativeMethod(JNIEnv *env, jobject obj, jint depth)
{
jclass cls = env->GetObjectClass(obj);
jmethodID mid = env->GetStaticMethodID(cls,"callback","(I)V");
if (mid == 0)
return;
printf("In C, depth = %d, about to enter Java\n", depth);
env->CallStaticVoidMethod(cls, mid, depth);
printf("In C, depth = %d, back from Java\n", depth);
}
Any idea what I'm doing wrong?

