0

I'm new to JNI and C++, here I'm going to pass an array from Java in Android to JNI, and in the JNI just need sort the array in Bubble way. However, it's seems this never get worked, I mean the array never get sorted. I don't know whether the array had never been passed or there're some problems with the functions in JNI.

And following is the demo-code.

File .h :

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_jni_jniDemo_JniInterface */

#ifndef _Included_com_jni_jniDemo_JniInterface
#define _Included_com_jni_jniDemo_JniInterface
#ifdef __cplusplus

extern "C" {
#endif

/*
 * Class:     com_jni_jniDemo_JniInterface
 * Method:    getString
 * Signature: ()Ljava/lang/String;
 */

JNIEXPORT jintArray JNICALL Java_com_jni_jniDemo_JniInterface_arraySort 
(JNIEnv *, jobject, jintArray);

#ifdef __cplusplus
}
#endif
#endif

File .cpp :

int sort(int &a, int &b){
    a = a + b;
    b = a - b;
    a = a - b;
}

JNIEXPORT jintArray JNICALL 
Java_com_jni_jniDemo_JniInterface_arraySort(JNIEnv *env, jobject obj, jintArray jArr) {

    jint *arr = env -> GetIntArrayElements(jArr, 0);
    int len = env -> GetArrayLength(jArr);

    for(int i = 0; i < len; i++){
        for(int j = 0; j < len - i -1; j++){
            if(arr[j] > arr[j + 1]){
                sort(arr[j], arr[j + 1]);
            }
        }
    }

    env -> ReleaseIntArrayElements(jArr, arr, JNI_COMMIT);

    return jArr;
}

File .java :

private int[] array = {7, 34, 2, 44, 6, 0, 127, 9};
private int[] newArray;    

for(int i = 0; i < array.length; i++){
    System.out.println("Before sort:" + String.valueOf(array[i]));
}

jniInterface = new JniInterface();
newArray = jniInterface.arraySort(array);

for(int j = 0; j < newArray.length; j++){
    System.out.println("After sort:" + String.valueOf(newArray[j]));
}

Any ideas?

8
  • 1
    JNIEXPORT jstring JNICALL Java_com_jni_jniDemo_JniInterface_arraySort - Looks like you've got the return type specified as jstring. Commented Apr 5, 2017 at 3:26
  • Plus, your sort function makes my head hurt. Commented Apr 5, 2017 at 3:27
  • 1
    @Mike M I'm so sorry, it's really my mistake! Thanks! Commented Apr 5, 2017 at 4:59
  • @Mike M I had edit my problem! This time I got another problem. Commented Apr 5, 2017 at 5:29
  • 1
    "This time I got another problem". What problem? You need to be more specific. Also, since you seem to be using jArr as an in/out-parameter, why does your jni function need to return anything at all? Commented Apr 5, 2017 at 5:41

1 Answer 1

0

Take a look here to see how to handle arrays in JNI. This sample will show you how to pass array back and forth:

http://jnicookbook.owsiak.org/recipe-No-013/

Note that you don't want to return the values as function result. When you release with JNI_COMMIT, values are available in the object you have passed as argument.

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

2 Comments

Thanks so much, I know where's the problem now.
Great! Thanks for the comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.