Vectors in JNI... Any Victors???
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Victor_Chinnappan
Posted On:   Friday, May 11, 2001 12:17 PM

Hi, Greets. I have a simple task: To manipulate a Java vector object from JNI. The following is an excerpt from my test application. I am having trouble with invoking the "get" method, to being with... Any pointers to solve this will be greatly appreciated... TIA, Victor.C /* envPtr -> Java Environment pointer. thisObject -> Usual this pointer. vecStrings -> java/lang/Vector */ Some_JNI_Method( JNIEnv* envPtr, jobject thisobject, jobject vecStrings) { //The following two lines seem to work fine.... jclass vecClazz = pjEnv->FindClass("java/uti   More>>

Hi,


Greets. I have a simple task: To manipulate a Java vector object from JNI.


The following is an excerpt from my test application. I am having trouble with invoking the "get" method, to being with...


Any pointers to solve this will be greatly appreciated...


TIA,


Victor.C


			
/*
envPtr -> Java Environment pointer.
thisObject -> Usual this pointer.
vecStrings -> java/lang/Vector
*/
Some_JNI_Method( JNIEnv* envPtr, jobject thisobject, jobject vecStrings)
{

//The following two lines seem to work fine....
jclass vecClazz = pjEnv->FindClass("java/util/Vector");
jmethodID jmethodGet = pjEnv->GetMethodID(vecClazz, "get", "(I)Ljava/lang/String;");

//This line is not a happy camper....
jobject item = pjEnv->CallObjectMethod(vecStrings, jmethodGetItem, 0);
}


I thought i am wrong in specifying the signature of the "get" method and tried
jmethodID jmethodGet = pjEnv->GetMethodID(vecClazz, "get", "(I)Ljava/lang/Object;");
but it is not happy too.

   <<Less

Re: Vectors in JNI... Any Victors???

Posted By:   Alexander_Krapf  
Posted On:   Monday, May 14, 2001 07:53 AM

Hi Victor,


you have more than one problem. The first is, that the Vector class does not declare a get method. You have to use elementAt instead.


The second is, that you're indeed right in assuming that you have to use the (I)Ljava/lang/Object; signature. You always have to use the exact signature of the Java method.

The third is, that you're not doing any error checking or you would have detected that the methodID is 0.

If you were using our JunC++ion tool instead, you would have imported the Vector class and your own class and generated C++ proxy bindings for them. You would then have been faced with the following native implementation task:



void _cmj_Java_some_JNI_Method( jcpp_localenv * pEnv, java::lang::Object & _this, java::util::Vector & vecStrings )
{
java::lang::Object obj = vecStrings.elementAt( 0 );
java::lang::String str = String::dyna_cast( obj );

if( str != null )
{
//do something with it
}
else
throw IllegalArgumentException( "bad vector" );
}


Much nicer, right? You can find out more about JunC++ion at http://www.codemesh.com.


Good luck,


Alex

About | Sitemap | Contact