Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning jint array from c to java through jni

I have created an integer array in java and passed the array to a cpp programme through jni My Code is:

import java.util.*;

class SendArray {
  //Native method declaration
  native int[] loadFile(int[] name);
  //Load the library
  static {
    System.loadLibrary("nativelib");
  }

  public static void main(String args[]) {
    int arr[] = {1,2,3,4,5,6,7,8,9,10};
    //Create class instance
    SendArray mappedFile=new SendArray();
    //Call native method to load SendArray.java
    int[] buf = mappedFile.loadFile(arr);
    //Print contents of SendArray.java
    for(int i=0;i<buf.length;i++) {
      System.out.print(buf[i]);
    }
  }
}

In cpp programme I am reversing the array and returning the array back to java programee My Code is::

#include <iostream>

using namespace std;

JNIEXPORT jintArray JNICALL Java_SendArray_loadFile
  (JNIEnv * env, jobject jobj, jintArray array) {
          cout<<"Orignal Array is:"<<endl; 
          int i;
          jboolean j;
          int ar[100];
          // for(i = 0; i < 10; i++){
          int * p= env->GetIntArrayElements(array, &j);
          //jint *array=env->GetIntArrayElements(one, 0);
          //ar[i] = array[i];
          //}

          for(i = 0 ; i < 10 ; i++){
            cout << p[i];
          }

          for(i = 10 ; i > 0 ; i--){
            ar[10-i] = p[i];
          }
          jintArray ret = env->NewIntArray(10);

          for(i = 0; i >10 ; i++){
            ret[i]=ar[i];
          }
          return ret;
}

error I am gettin is:

error: no match for 'operator=' in '*(ret +((long unsigned int)((long unsigned int)i))) = ar[i]'

What should I do to return the array back to java programme???? please help!!!!!

like image 913
user1557217 Avatar asked Jul 27 '12 10:07

user1557217


People also ask

How do I return an array from JNI?

If you just wanted to return a single dimensional array of ints, then the NewIntArray() function is what you'd use to create the return value. If you wanted to create a single dimensional array of Strings then you'd use the NewObjectArray() function but with a different parameter for the class.

What is Jclass in JNI?

The jclass instance is your object on which a method will be invoked; you'll need to look up the getName method ID on the Class class, then invoke it on the jclass instance using CallObjectMethod to obtain a jstring result. So in short yes, you just call the getName function and look at the jstring result.

How JNI works?

It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++). JNI is vendor-neutral, has support for loading code from dynamic shared libraries, and while cumbersome at times is reasonably efficient.

What is JNIEnv * env?

jobject NewGlobalRef(JNIEnv *env, jobject obj); Creates a new global reference to the object referred to by the obj argument. The obj argument may be a global or local reference. Global references must be explicitly disposed of by calling DeleteGlobalRef() .


1 Answers

Change your native code to this instead:

JNIEXPORT jintArray JNICALL Java_SendArray_loadFile(JNIEnv *env, jobject obj, jintArray oldArray) {
    const jsize length = env->GetArrayLength(oldArray);
    jintArray newArray = env->NewIntArray(length);
    jint *oarr = env->GetIntArrayElements(oldArray, NULL);
    jint *narr = env->GetIntArrayElements(newArray, NULL);

    for (int o = 0, n = length - 1; o < length; o++, n--) {
        narr[n] = oarr[o];
    }

    env->ReleaseIntArrayElements(newArray, narr, NULL);
    env->ReleaseIntArrayElements(oldArray, oarr, NULL);

    return newArray;
}

Your main problem was that you tried to manipulate the ret object directly and that is not possible. You have to use JNI functions to manipulate a jintArray object.

And you also have to make sure you release your objects when done with them.

like image 103
maba Avatar answered Oct 29 '22 15:10

maba