convert vector to jobject in C++/JNI?

前端 未结 2 1717
情歌与酒
情歌与酒 2020-12-10 02:43

I am using Java native function -

public native ArrayList parseXML();

In C++ my native function -

vector<         


        
相关标签:
2条回答
  • 2020-12-10 02:50

    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; 
    
    0 讨论(0)
  • 2020-12-10 02:51

    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

    0 讨论(0)
提交回复
热议问题