4

I am new to Java programming and have a basic question. I would like to pass a string array to JNI layer. Inside the JNI function can the array be filled with strings? All the posts either talk how to return a string array from JNI layer or how parse a string array in JNI layer. Is it possible?

Thanks

ravi raj
  • 57
  • 4
  • You may refer any existing jni launcher code. Did you have a look at http://git.eclipse.org/c/equinox/rt.equinox.framework.git/tree/bundles/org.eclipse.equinox.executable/ – Jayan Feb 23 '13 at 13:32

1 Answers1

3

Yes, it is possible. There is an example here that does exactly what you want. You will need to create the array and the strings from the JNI side (akin of invoking new String(char[])) in java. Both Unicode and UTF-8 are supported (there is a JNI function set for each one).

#include <jni.h>
#include "ArrayHandler.h"

JNIEXPORT jobjectArray JNICALL Java_ArrayHandler_returnArray
  (JNIEnv *env, jobject jobj)
{
    jobjectArray ret;
    int i;

    char *message[5]= {"first",
                       "second",
                       "third",
                       "fourth",
                       "fifth"};

    ret= (jobjectArray)env->NewObjectArray(5,
                                           env->FindClass("java/lang/String"),
                                           env->NewStringUTF(""));

    for(i=0;i<5;i++) {
        env->SetObjectArrayElement(
                    ret,i,env->NewStringUTF(message[i]));
    }

    return(ret);
}
sashoalm
  • 75,001
  • 122
  • 434
  • 781
Javier
  • 12,100
  • 5
  • 46
  • 57
  • 1
    I'd like to add a general warning from building complexer data structures for Java in JNI. This could bring you into hot water if you don't do the things you're expected to do by hand in the JNI code properly. Just as an example: The linked listing has some serious flaws: After calling `NewObjectArray` it should have been checked if `ExceptionOccured`. Same is true for creating new string objects. Of course somtimes there is no way around doing this, but in this case keep in mind that you are moving in a minefield. – junix Feb 23 '13 at 13:44
  • Thanks the reply. Is this valid code?? if the displayarray has already been allocated memory in java layer. The example only talks about returning a newly constructed string array from JNI. JNIEXPORT jboolean JNICALL Java_***(JNIEnv *env, jobject obj, jobjectArray displayArray) { int stringCount = (env)->GetArrayLength(displayArray); for(int i = 0; i < stringCount; i++) { jstring string = (jstring) (env)->GetObjectArrayElement(displayArray,i);jstring tempString = (env)->NewStringUTF("Hello"); string = tempString; free(tempString); } return false; } – ravi raj Feb 23 '13 at 14:13
  • See http://stackoverflow.com/questions/5859673/should-you-call-releasestringutfchars-if-getstringutfchars-returned-a-copy for the correct way of releasing java strings in JNI code. (In short, the JNI mechanism may allocate additional memory for your string, that must be explicitly released). – Javier Feb 23 '13 at 14:22
  • @raviraj Strings are assumed immutable in Java. So you have to create new strings anyway. If the array is already pre allocated, you can just skip the `NewObjectArray` part and pass the reference to the array to the function. Or have I misunderstood your question? – junix Feb 24 '13 at 08:02
  • @junix String array is already pre allocated but the data is not filled. I am expecting the lower layers fill the array appropriately. That is the intention. Is the statement string = tempString correct if the strings are immutable?? – ravi raj Feb 24 '13 at 18:41