Need help on returning arrays from C++ to Java
843829Dec 13 2002 — edited Feb 13 2004Hi all, I hava a C++ application that contains a method that will return a float array to Java. I was able to write the Java Code, the JNI code to call the C++ method and compile them successfully. However on calling the native method from Java, the data in the array returned was not what I want. Please look through my code and see what is wrong with it.
My Java Code:
public native float[] Statistics(int inputKey); // This is the native method
public float[] Stats(int k){ //Java method to call the native method so that I can use it in a Bean for a JSP.
inputKey = k;
float[] p = new float[4];
p = Statistics(inputKey);
return p;
}
For this native method, an integer is passed from Java to the native side and the native method will use this integer to generate a float array size of 4 and populate it with data and return it back to Java.
My JNI code
extern "C"
JNIEXPORT jfloatArray JNICALL
Java_com_jspsmart_upload_FinalWaterMark_Statistics(JNIEnv *env, jobject obj, jint inputKey)
{
jfloatArray floatArray = env->NewFloatArray(4); //This creates a new Java floatArray to store the C++ array
CWatermarker2App p; // This is the C++ class to be used
jfloat *stats = p.OnWatermarkStatistics(inputKey) // The C++ method returns a float pointer for an array of size 4
env->SetFloatArrayRegion(floatArray, 0 , 4 , stats); //storing the C++ float array into the Java float Array
return floatArray; // return the Array to Java
}
In this example, I should have the data returned as [4952.0 2529.0 1706.0 33.6] in the array.
But i am getting junk instead like 2.53E-23, 1.402E-15 etc..
Is this a type conversion mistake?
Please help!