Returning vector to Java from c++
1 posts in topic
Flat View  Flat View

Posted By:   Srini_b
Posted On:   Friday, June 30, 2006 11:36 AM


I have two stl vectors in my c++ code and I need to return them to java code and use them there.How can I do this.

Re: Returning vector to Java from c++

Posted By:   Anonymous  
Posted On:   Tuesday, July 11, 2006 03:49 PM

You can't do that, first of all, unfortunelly, you can only return one object in a method call (maybe you can do that using references in Java, i'm not sure if this will work). What you can do is create a wrapper function, that wraps all the elements in the stl vector class to your newly created java.util.Vector java class.



vector vec;


jclass clazz = (*env).FindClass("java/util/Vector");

jobject obj = (*env).NewObject(clazz, (*env).GetMethodID(clazz, "", "()V"));


for (int n=0;n

   char* str = (char*) static_cast(vec[n]);


   jstring _str = (*env).NewStringUTF(str);

   (*env).CallVoidMethod(object, (*env).GetMethodID(clazz, "addElement", "(java/lang/Object)V"), _str);




return obj;
About | Sitemap | Contact