Passing array of strings from .c file to java dll via JNI
I trying to get C# to instanciate a class in my java program. I have had a number of simpler examples working but I've hit the problem that my java class takes a string array as the constructor.
My question is how in a .c file do you create an string array that can then be passed through to the java class?
At the minute I've managed to pass across a single jString object.
Heres the method in my .c file.
jobject newClassInstance()
{
jmethodID MID_init;
jobject obj;
jstring name;
jobjectArray ret;
MID_init = (*env)->GetMethodID (env, jClass, "<init>", "([Ljava/lang/String;)V");
if (!MID_init) {
printf("Error: dllClass.<init>() not found\n");
return NULL;
}
name = (*env)->NewStringUTF(env,"Moo");
obj = (*env)->NewObject(env, jClass, MID_init, name);
if (!obj) {
printf("Error开发者_如何学JAVA: failed to allocate an object\n");
return NULL;
}
return obj;
}
Please let me know if you need anymore information.
Kind Regards
Ash
- First create the array of strings.
- Create every Element String.
- Call Java Initializer with Array object.
Sample Code:
jobjectArray stringArray;
jString tmp;
char *stringA = "Test1";
char *stringB = "Test2";
jclass clsString;
jint size = 2;
clsString = (*env)->FindClass(env, "java/lang/String");
stringArray = (*env)->NewObjectArray(env, size, clsString, 0);
tmp = (*env)->NewStringUTF(env, stringB);
(*env)->SetObjectArrayElement(env, stringArray, 0, tmp);
tmp = (*env)->NewStringUTF(env, stringA);
(*env)->SetObjectArrayElement(env, stringArray, 1, tmp);
obj = (*env)->NewObject(env, jClass, MID_init, stringArray);
精彩评论