JNI problem : from c Languege call Java

199 views
Skip to first unread message

zhangho

unread,
Oct 15, 2009, 5:24:50 AM10/15/09
to android-ndk
JNI problem : from c Languege call Java

i think from c Languege call Java,
but that CallIntMethod() return value is not equals getIpv6() -> 99,
CallIntMethod() its always return strange value is [-1091242504] or
[11212] .

additional i already slove [Method not found: 'getIpv6' '(I;)I']
error.

please help me!


java Language:
public void onCreate() {
setConnecCallback2();
}

public static native void setConnecCallback2();

public int getIpv6(int ipv6){

Log.i("JNI", "Callback from Native Messenger to Java Hello Chat");

return 99;
}

static {
System.loadLibrary("mm");
}


cLanguage:
#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <pthread.h>


JNIEXPORT void JNICALL Java_com_ServersService_setConnecCallback2
(JNIEnv *env, jobject this) {

FILE *fd;
    fd = fopen("/sdcard/made.log", "w");

jclass jcCallback = (*env)->FindClass(env,"com.ServersService");

jmethodID mid = (*env)->GetMethodID(env,jcCallback,"getIpv6","(I)I");

int ii = (int)(*env)->CallIntMethod(env, jcCallback ,mid, 444 );


fprintf(fd, "iii is : [%d] \n", ii);
fclose(fd);

return;
}

zhangho

unread,
Oct 15, 2009, 5:26:05 AM10/15/09
to android-ndk

zhangho

unread,
Oct 15, 2009, 5:26:39 AM10/15/09
to android-ndk

koba

unread,
Oct 15, 2009, 9:09:56 AM10/15/09
to android-ndk

I guess,
int ii = (int)(*env)->CallIntMethod(env, jcCallback ,mid, 444 );
should be
int ii = (int)(*env)->CallIntMethod(env, this, mid, 444 );

Check the second argument of CallIntMethod.

fadden

unread,
Oct 15, 2009, 3:37:18 PM10/15/09
to android-ndk
On Oct 15, 6:09 am, koba <tetsu.k...@gmail.com> wrote:
> I guess,
>   int ii =  (int)(*env)->CallIntMethod(env, jcCallback  ,mid, 444 );
> should be
>   int ii =  (int)(*env)->CallIntMethod(env, this, mid, 444 );
>
> Check the second argument of CallIntMethod.

The second argument should be the target of the virtual call, not the
class object. However, the change above isn't sufficient.
"setConnecCallback2" is declared "public static native void
setConnecCallback2()" in Java, so the second argument to
setConnecCallback2 is actually "jclass clazz" rather than "jobject
this".

What's lacking here is an instance of "com.ServersService". It's
calling an instance method without an instance. I can't be sure from
just the code fragment, but it looks like declaring setConnecCallback2
as non-static in the Java source and applying the above change would
solve the problem.

Mathieu

unread,
Oct 16, 2009, 6:04:33 AM10/16/09
to andro...@googlegroups.com
According to http://java.sun.com/docs/books/jni/html/fldmeth.html c.f
4.2.3 you are just missing
jmethodID mid = (*env)->GetMethodID(env,jcCallback,"getIpv6","(I)I")
->
jmethodID mid =
(*env)->GetStaticMethodID(env,jcCallback,"getIpv6","(I)I")

(You just forget the static)

Good luck ;)
Reply all
Reply to author
Forward
0 new messages