Returning String[] from C program (JNI)

G

Gordon Beaton

How can I return a string[] from a cprogram to a java program? [...]
Can someone please suggest? I am not good at C.

Create the array like this:

jclass sclass = (*env)->FindClass(env, "java/lang/String");
jobjectArray arr = (*env)->NewObjectArray(env, n, sclass, NULL);

Now you've got an array with space for n Strings, but each position in
the array is empty.

To populate the array, use SetObjectArrayElement() to put a String
reference in each position.

Then simply:

return arr;

/gordon
 
A

Ann

How can I return a string[] from a cprogram to a java program?

private native String[] readRFIDData();

JNIEXPORT jobjectArray JNICALL Java_RfidDM_readRFIDData
(JNIEnv *, jobject);


Above is the definition of a jni method .
I have gone through the sample code, but they explain how to return arrays from C++.

Can someone please suggest? I am not good at C.

Much thanks,
 
Joined
Jun 20, 2007
Messages
1
Reaction score
0
/* JNI Function returning StringArray of size 6 */

JNIEXPORT jobjectArray JNICALL Java_RfidDM_readRFIDData
(JNIEnv *env, jobject obj)
{
jstring str = NULL;
int i;
jclass strCls = (*env)->FindClass(env,"Ljava/lang/String;");
jobjectArray strarray = (*env)->NewObejctArray(env,6,strCls,NULL);
for(i=0;i<6;i++)
{
str = (*env)->NewStringUTF(env,"VIPIN");
(*env)->SetObjectArrayElement(env,strarray,i,str);
(*env)->DeleteLocalRef(env,str);
}
return strarr;
}
/* ENJOY */
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top