Pass an array of objects from C++ to JAVA using JNI
843829Feb 7 2005 — edited Feb 8 2005Hi,
I'm having problem passing an array of objects from C++ to JAVA. I have a C++ struct:
struct RtEntry{
int routingID;
int MSTableIndex;
};
In my C++ program, all I try to do is assigning an array of RtEntry to jobjectArray, and then passing it to JAVA. However, I have problem setting the jobjectArray correctly. I get an exceptioin/coredump while running the program ("java ObjectArrayTest").
///////////////////////////////////////start of exception
Unexpected Signal : 10 occurred at PC=0xfe5fa278
Function name=JVM_IsNaN
Library=/cfs/tools/public/jdk1.3.1/sparc/j2sdk1_3_1/jre/lib/sparc/client/libjvm.so
Current Java thread:
at ObjectArrayTest.getRouteArray(Native Method)
at ObjectArrayTest.main(ObjectArrayTest.java:15)
Dynamic libraries:
0x10000 /cfs/tools/public/jdk1.3.1/sparc/j2sdk1_3_1/bin/../bin/sparc/native_threads/java
0xff350000 /lib/libthread.so.1
0xff390000 /lib/libdl.so.1
0xff200000 /lib/libc.so.1
0xff330000 /usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1
0xfe480000 /cfs/tools/public/jdk1.3.1/sparc/j2sdk1_3_1/jre/lib/sparc/client/libjvm.so
0xff2d0000 /lib/libCrun.so.1
0xff1d0000 /lib/libsocket.so.1
0xff100000 /lib/libnsl.so.1
0xff0d0000 /lib/libm.so.1
0xff300000 /lib/libw.so.1
0xff0b0000 /lib/libmp.so.2
0xff070000 /cfs/tools/public/jdk1.3.1/sparc/j2sdk1_3_1/jre/lib/sparc/native_threads/libhpi.so
0xff040000 /cfs/tools/public/jdk1.3.1/sparc/j2sdk1_3_1/jre/lib/sparc/libverify.so
0xfe440000 /cfs/tools/public/jdk1.3.1/sparc/j2sdk1_3_1/jre/lib/sparc/libjava.so
0xfe410000 /cfs/tools/public/jdk1.3.1/sparc/j2sdk1_3_1/jre/lib/sparc/libzip.so
0xfe230000 /homes/ctien/java/libObjectArrayTest.so
0xfd090000 /lib/libC.so.5
Local Time = Mon Feb 7 15:56:23 2005
Elapsed Time = 0
#
# HotSpot Virtual Machine Error : 10
# Error ID : 4F530E43505002CC 01
# Please report this error at
# http://java.sun.com/cgi-bin/bugreport.cgi
#
# Java VM: Java HotSpot(TM) Client VM (1.3.1-b24 mixed mode)
#
# An error report file has been saved as hs_err_pid6382.log.
# Please refer to the file for further information.
#
myjava: 6382 Abort(coredump)
//////////////////////////////////////////////////////////////////////end of exception
My trace tells me it core dumped at the line,
env->SetObjectArrayElement(result,i,(jobject)&rtArray);
I have checked the result and the rtArray, and they are both NOT NULL.
Does anyone know why I am getting this error? My C++
and JAVA programs are showed below. Another thing I try
to do is to cast the array of object from C++ in JAVA. I am not sure
if I can pass an array of object defined in C++ to JAVA and
cast the object in JAVA with a similar structure like
in this case (I have a RtEntry structure in C++ and a RtEntry structure
in JAVA). Both structures have two member data - routingID and
MSTableIndex. I greatly appreciate any input/help from you. Thanks.
///////////////////////////////////////////////////////////////////////// start of C program
#include <jni.h>
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include "ObjectArrayTest.h"
#include "RtEntry.H"
extern "C"
JNIEXPORT jobjectArray JNICALL
Java_ObjectArrayTest_getRouteArray(JNIEnv *env,
jclass cls)
{
RtEntry rtArray[10];
jobject x;
int size = 10;
jobjectArray result;
int i;
jclass rtArrCls = env->FindClass("RtEntry");
if (rtArrCls == NULL) {
return NULL; /* exception thrown */
}
result = env->NewObjectArray(size, rtArrCls,
NULL);
if (result == NULL) {
return NULL; /* out of memory error thrown */
}
for (i = 0; i < size; i++)
{
rtArray[i].routingID = 12345+i*10000;
rtArray[i].MSTableIndex = 6+i;
env->SetObjectArrayElement(result,i,(jobject)&rtArray[i]);
}
return result;
}
/////////////////////////////////////////end of C++ program
/////////////////////////////////////////start of JAVA program
class ObjectArrayTest {
private static native RtEntry[] getRouteArray();
public static void main(String[] args) {
RtEntry rtEntries[];
rtEntries = new RtEntry[10];
for (int i = 0; i < 10; i++)
{
rtEntries[i] = new RtEntry(1000*(i+1), 3*(i+1));
}
rtEntries = getRouteArray();
}
static {
System.loadLibrary("ObjectArrayTest");
}
}
class RtEntry extends Object {
int routingID;
int MSTableIndex;
public RtEntry(int id, int index)
{
setRtEntry(id,index);
}
public void setRtEntry(int id, int index)
{
routingID = id;
MSTableIndex = index;
}
}
/////////////////////////////////end of JAVA program