I am using Java native function -
public native ArrayList<String> parseXML();
In C++ my native function -
vector<string> resultList;
JNIEXPORT jobject JNICALL Java_Sample1_parseXML
(JNIEnv *env, jobject obj){
// logic
return resultList; // here getting error
}
My problem is that how to convert resultList (vector type) to jobject type?
You would have to create a wrapper for the ArrayList in C++. Something like:
vector <char*> vec;
jclass clazz = (*env).FindClass("java/util/ArrayList");
jobject obj = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "<init>", "()V"));
for (int n=0;n<vec.size();n++)
{
char* str = (char*) static_cast<char*>(vec[n]);
jstring _str = (*env).NewStringUTF(str);
(*env).CallVoidMethod(object, (*env).GetMethodID(clazz, "add", "(java/lang/Object)V"), _str);
}
return obj;
for further information see:
http://download.oracle.com/javase/1.4.2/docs/guide/jni/spec/functions.html
The method is:
bool add(Object);
So the signature is:
"(Ljava/lang/Object;)Z"
More at: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp276
vector <char*> vec;
jclass clazz = (*env).FindClass("java/util/ArrayList");
jobject obj = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "<init>", "()V"));
for (int n=0;n<vec.size();n++)
{
char* str = (char*) static_cast<char*>(vec[n]);
jstring _str = (*env).NewStringUTF(str);
(*env).CallBooleanMethod(object, (*env).GetMethodID(clazz, "add", "(Ljava/lang/Object;)Z"), _str);
}
return obj;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With