9

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!!!!!

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't work on (at least) java 8, should be like (*env)->GetIntArrayElements(env, oldArray, 0), others methods should me modified accordingly.
@Fynn No it will work perfectly well with a C++ compiler. Your notation is used for Ansi C.
Hi, I am executing the below code snippet vector<int> vec_data = {1,2,3,4,5}; jintArray jarray = env->NewIntArray(vec_data.size()); jint* elements = env->GetIntArrayElements(jarray, 0); for(int i=0; i<vec_data.size(); i++){ elements[i] = vec_data[i]; cout<<elements[i]<<endl; cout<<jarray[i]<<endl; } But i could see that jarray is always printing 0 or some grabage value, am i missing something here? Basically i want to return jintArray from the vector.

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.