Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can NOT pass java int to jni function

I have a simple jni function in test.cpp:

#include <jni.h>
#include <stdio.h>

extern "C" {

JNIEXPORT jint JNICALL Java_dri_put(JNIEnv* env, jstring js, jint ji){

    printf("%d \n", ji);
    int t = ji;
    printf("%d \n", t);
    int k = -3412;
    return k;
 }
 }

my java class javatest.java:

public class javatest {
  public static void main(String args[]) {
    System.loadLibrary("test");
    int t = 134;
    int k = dri.put("a", 5641);
    System.out.println(k);
  }
 }

the output just prints some random number of the passing integer:

1075968840

1075968840

-3412

however if i change jint to jdouble and pass java double variable, it works fine, appreciate any help here.

The dri java class is:

public class dri
{
  public final static native int put(String jarg1, int jarg2);
}

sizof(int) results in 4 bytes on my machine (red-hat)

like image 974
xichblueagle Avatar asked May 24 '12 15:05

xichblueagle


1 Answers

Your signature is incorrect (did you use javah?). The second argument to a JNI function will be the object (for object methods) or the class (for static class methods).

Your declaration should look like this instead:

JNIEXPORT jint JNICALL Java_dri_put(JNIEnv* env, jclass cls, jstring js, jint ji);
like image 104
technomage Avatar answered Sep 30 '22 02:09

technomage