David Sof
David Sof

Reputation: 21

c JNI call java function

My first function code in c jni is

void fun1()
{
   jmethodID java_fun = (*global_env)->GetMethodID(global_env,cls_Env, "test1", "()V");
   (*global_env)->CallVoidMethod(global_env,(*global_obj), java_fun);
}

And my second is

void fun2(int x)
{
   jmethodID java_fun = (*global_env)->GetMethodID(global_env,cls_Env, "test2", "([I)V");
   (*global_env)->CallVoidMethod(global_env,(*global_obj), java_fun,(int)x);
}

In my java side the code for first c function is

private void test1()
{
    System.out.printf("test1");
    callfunction();
}
void callfunction()
{
   System.out.printf("how i can get here??");

}

The first question is how can i get to callfunction() from C call?

Now the second java code for second c function is

public int var1;
private void test2(int x)
{
    System.out.printf("test2");
    var1=x;
}

The second question is how can i save my data from c side in java side?

Thanks

Upvotes: 1

Views: 151

Answers (2)

mesh
mesh

Reputation: 937

Q1:

void fun1()
{
   jmethodID java_fun = (*global_env)->GetMethodID(global_env, cls_Env, "test1", "()V");
   (*global_env)->CallVoidMethod(global_env, global_obj, java_fun);

}

Q2:
 void test2(int x)
{
    System.out.printf("test2");
    var1=x;
}

void fun2()
{
   int i = 2;
   jmethodID java_fun = (*global_env)->GetMethodID(global_env, cls, "test2", "(I)V");
   (*global_env)->CallVoidMethod(global_env, global_obj, java_fun, i);

}

Please check this link

Upvotes: 0

Alex Cohn
Alex Cohn

Reputation: 57173

There is a typo in fun2(): it should read GetMethodID(global_env, cls_Env, "test2", "(I)V"); (the [ is wrong).

If you want to call callfunction() from C directly, use

GetMethodID(global_env, cls_Env, "callfunction", "()V");

Upvotes: 1

Related Questions