Skip to Main Content

Retrieving arrays of java class inside C++ code

860480May 14 2011 — edited May 16 2011
Hi everyone,

Currently i am using JNI for native access. But i am getting some error while retrieving arrays of a java class inside my c++ code. I mean, if my java code is:
package GUI;
public class myClass {
    
    public native test1 classcl();       ///return type is object of class 'test1' (defined below)
    
        public static void main(String[] args)
    {
        System.load("D:\\dot_net\\TestGUI\\debug\\TestGUI.dll");
        myClass obj = new myClass();
        obj.classcl();
        
    }

}

class test1 {
    public int var = 90;
    public byte[] raw = new byte[5];
    
    public byte[] GetByte(){
        return raw;  
}}
then my corresponding implementation in C++ would be:
 JNIEXPORT jobject JNICALL Java_GUI_myClass_classcl
  (JNIEnv *env, jobject obj)
{
	jclass testC;
	jobject objC;
	jint var1;
	jfieldID fid1;

	testC = env->FindClass("GUI/test1");
	fid1 = env->GetFieldID(testC,"var","I");
        objC = env->AllocObject(testC);
        env->SetIntField(objC,fid1,74);             ///// by this the integer field 'var' is accessed and it's value is changed
        return objC;
} 
/*****************DOUBT******************\
You all must have noticed that there is also a byte array named raw inside class test1.
So, my doubt is like I accessed the integer field var of class test1, if i also have to access the byte array(raw) and change it's value, then what would be my code?

I have tried very hard, but somehow not able to achieve this.

Thanking in advance,

Satya Prakash.

Edited by: 857477 on May 14, 2011 3:29 AM
Comments
Post Details
Added on May 14 2011
5 comments
182 views