1

I am trying to send an integer value from the jni c code to a java function. But as I try to do this , JVM crashes. I don't know the reason. Following codes tell how I have been trying to achieve this.

Java Code :

class Tester {
public native void func();
public native void func_1(Tester T);
public native void func_2(String S);

public static void main(String args[]) {
   Tester tester = new Tester();
   tester.func();
}

public void printInteger(int x) {
    System.out.println(x);
}

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

JNI C Code:

#include <stdio.h>
#include "Tester.h"

void Java_Tester_func
(JNIEnv *env, jobject obj) {
  jclass cls = (*env)->GetObjectClass(env,obj);
  jmethodID mid = (*env)->GetMethodID(env,cls,"printInteger","(I)V");
  jvalue *a1;
  a1->i = 2;
  (*env)->CallVoidMethodA(env,obj,mid,a1);
 }

What mistake have I made ?

1
  • 6
    a1 is an unallocated pointer. Commented Oct 6, 2012 at 5:08

1 Answer 1

1

Try this :

void Java_Tester_func
(JNIEnv *env, jobject obj) {
  jclass cls = (*env)->GetObjectClass(env,obj);
  jmethodID mid = (*env)->GetMethodID(env,cls,"printInteger","(I)V");
  jvalue a1,*arr;
  a1.i = 2;
  arr = &a1;
  (*env)->CallVoidMethodA(env,obj,mid,arr);
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.