How can I call an array of object through Native method?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Nitin_Gupta
Posted On:   Friday, September 7, 2001 05:49 AM

I have written the class Employee and Department as follows: class Employee { int EmpID; double Salary; } class Department { int EmpNo; Employee Emp[] = new Employee [3]; } I have declared a native method named GetTotalSalary() in the class named Native as follows: public class Native { public native static void GetTotalSalary(Department dept); static { System.loadLibrary("SampleNative"); } } The G   More>>

I have written the class Employee and Department as follows:


class Employee

{

int EmpID;

double Salary;

}





class Department

{

int EmpNo;

Employee Emp[] = new Employee
[3];

}



I have declared a native method named GetTotalSalary() in the class named Native as follows:



public class Native

{

public native static void GetTotalSalary(Department dept);

static

{

System.loadLibrary("SampleNative");

}

}




The GetTotalSalary() Method will not return any thing, but it will show the Total salary of all the Employees of the Department in a MessageBox. This method has to be implemented in VC++ or C++.





My Application CallNative is as follows:




public class CallNative

{

Native nat = new Native();

Department Dept = new Department();


public static void main(String args[])

{

CallNative call = new CallNative();

call.FillData();

call.nat.GetTotalSalary(call.Dept);

}


public void FillData()

{


Dept.EmpNo = 3;


Dept.Emp[0] = new Employee();

Dept.Emp[1] = new Employee();

Dept.Emp[2] = new Employee();


Dept.Emp[0].EmpID = 101;

Dept.Emp[0].Salary = 5000.0;


Dept.Emp[1].EmpID = 302;

Dept.Emp[1].Salary = 16000.0;


Dept.Emp[2].EmpID = 303;

Dept.Emp[2].Salary = 17000.0;

}


}





I got the follwing signature of the native method while I run javah:



JNIEXPORT void JNICALL Java_Native_GetTotalSalary
(JNIEnv *, jclass , jobject );


I am unable to access the elements of Employee class. Please Help me and implement the above function in VC++ or C++.





Thanks,


Nitin Gupta
   <<Less

Re: How can I call an array of object through Native method?

Posted By:   Alexander_Krapf  
Posted On:   Saturday, September 8, 2001 07:52 AM

Nitin,


You need to write some serious JNI if you want to access the data based on the Department that is being passed in.


First, you need to use JNI to access the two fields, then you need to use JNI to access the array elements.


Get a good book on JNI (I recommend Sheng Liang or Rob Gordon's books, which I believe are on JGuru's literature references) or get our JunC++ion tool (not free).


JunC++ion generates C++ proxy classes for Java classes and you can use them in the implementations of native methods. Your Visual C++ implementation of the native method would look like this:


jdouble total = 0;

for( jint i=0; i total += dept.Emp[ i ].Salary;

MessageBox( ... );

That would be much easier to write and to maintain than doing it in JNI. Whether your problem warrants the purchase of our tool is another question.


You can find out more about JunC++ion at http://www.codemesh.com.


Good luck,


Alex

About | Sitemap | Contact