M
markryde
Hello,
I have a simple java class named MyClass, which has only 2 members and
a ctor:
public class MyClass
{
public int i;
public int j;
public MyClass(int i,int j)
{
this.i=i;
this.j=j;
}
}
Now, in a JNI method I want to create an array of objects of this class
and return it to a java method which calls this JNI method.
I tried this :
in the JNI native:
JNIEXPORT jobjectArray JNICALL Java_Test_getMyArray (JNIEnv* env,
jobject obj)
{
jobjectArray joaMyArray;
jobject newObject;
jclass myClass = (*env)->FindClass(env,"MyClass");
if (myClass==NULL)
{
return NULL;
}
jmethodID midCtor = (*env)->GetMethodID(env,myClass, "<init>",
"(II)V");
if (midCtor==NULL)
return NULL;
joaMyArray = (*env)->NewObjectArray(env,2, myClass, NULL);
newObject = (*env)->NewObject(env,myClass, midCtor,1,2);
newObject = (*env)->NewObject(env,myClass, midCtor,3,4);
(*env)->SetObjectArrayElement(env,joaMyArray, 0, newObject);
(*env)->SetObjectArrayElement(env,joaMyArray, 1, newObject);
return ret;
}
Now , in the Java class I want to retrieve the objects of this array.
I tried:
public static void main(String[] args)
{
Test test = new Test();
Object[] myClass = test.getMyArray();
(when I tried :
MyClass[] myClass = test.getMyArray();
it gave compilation error,saying that it expects Object[].)
I want to iterate over the elements of the array of MyClass
isntances, which is returned from getMyArray, and print the i,j
values of the elements of this array. (which are instances of MyClass)
How can I do it ?
Regards,
-- MR
I have a simple java class named MyClass, which has only 2 members and
a ctor:
public class MyClass
{
public int i;
public int j;
public MyClass(int i,int j)
{
this.i=i;
this.j=j;
}
}
Now, in a JNI method I want to create an array of objects of this class
and return it to a java method which calls this JNI method.
I tried this :
in the JNI native:
JNIEXPORT jobjectArray JNICALL Java_Test_getMyArray (JNIEnv* env,
jobject obj)
{
jobjectArray joaMyArray;
jobject newObject;
jclass myClass = (*env)->FindClass(env,"MyClass");
if (myClass==NULL)
{
return NULL;
}
jmethodID midCtor = (*env)->GetMethodID(env,myClass, "<init>",
"(II)V");
if (midCtor==NULL)
return NULL;
joaMyArray = (*env)->NewObjectArray(env,2, myClass, NULL);
newObject = (*env)->NewObject(env,myClass, midCtor,1,2);
newObject = (*env)->NewObject(env,myClass, midCtor,3,4);
(*env)->SetObjectArrayElement(env,joaMyArray, 0, newObject);
(*env)->SetObjectArrayElement(env,joaMyArray, 1, newObject);
return ret;
}
Now , in the Java class I want to retrieve the objects of this array.
I tried:
public static void main(String[] args)
{
Test test = new Test();
Object[] myClass = test.getMyArray();
(when I tried :
MyClass[] myClass = test.getMyArray();
it gave compilation error,saying that it expects Object[].)
I want to iterate over the elements of the array of MyClass
isntances, which is returned from getMyArray, and print the i,j
values of the elements of this array. (which are instances of MyClass)
How can I do it ?
Regards,
-- MR