Programs from: Sheng Liang, The Java Native Interface, Programmer's
Guide and Specification, The Java Series, (c) 1999, pp. 38-39.
I thought I copied the program pretty exactly, but I don't know
why I am getting an ArrayIndexOutOfBoundsException.
I tried to switch the jint size to int size in the function declaration
prototype, but that didn't make any difference. I tried the "i" for loop
with a range of i = 1 ; i <= size ; but that didn't help.
Any help would be appreciated. Thanks.
David Stevenson
bash -v buildObjectArrayTest.sh
javac ObjectArrayTest.java
javah -jni ObjectArrayTest
gcc -c -ansi -shared -I/usr/java/j2sdk1.4.2_06/include
-I/usr/java/j2sdk1.4.2_06/include/linux -I-. ObjectArrayTest.c
gcc -shared ObjectArrayTest.o -o libObjectArrayTest.so
java -Djava.library.path=. ObjectArrayTest
Java_ObjectArrayTest_initInt2DArray entered
Java_ObjectArrayTest_initInt2DArray after FindClass
Java_ObjectArrayTest_initInt2DArray before NewObjectArray
Java_ObjectArrayTest_initInt2DArray after NewObjectArray
size: 3
i: 0
before NewIntArray
after NewIntArray
before SetIntArrayRegion
before SetObjectArrayElement
before DeleteLocalRef
after DeleteLocalRef
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at ObjectArrayTest.initInt2DArray(Native Method)
at ObjectArrayTest.main(ObjectArrayTest.java:7)
$ more ObjectArrayTest.java
public class ObjectArrayTest
{
private static native int [] [] initInt2DArray ( int size ) ;
public static void main ( String [] args )
{
int [] [] i2arr = initInt2DArray ( 3 ) ;
for ( int i = 0 ; i < 3 ; i++ )
{
for ( int j = 0 ; j < 3 ; j++ )
{
System.out.print ( " " + i2arr[i][j] ) ;
}
System.out.println () ;
}
}
static
{
System.loadLibrary ( "ObjectArrayTest" ) ;
}
}
#include <jni.h>
#include <stdio.h>
#include "ObjectArrayTest.h"
/*
* Class: ObjectArrayTest
* Method: initInt2DArray
* Signature: (I)[[I
*/
JNIEXPORT jobjectArray JNICALL
Java_ObjectArrayTest_initInt2DArray ( JNIEnv *env, jclass cls, int size )
{
jobjectArray result ;
int i ;
printf ( "Java_ObjectArrayTest_initInt2DArray entered\n" ) ;
jclass intArrCls = (*env)->FindClass ( env, "[I" ) ;
printf ( "Java_ObjectArrayTest_initInt2DArray after
FindClass\n" ) ;
if ( intArrCls == NULL )
return NULL ; /* exception thrown */
printf ( "Java_ObjectArrayTest_initInt2DArray before
NewObjectArray\n" ) ;
result = (*env)->NewObjectArray ( env, size, intArrCls, NULL ) ;
if ( result == NULL )
return NULL ; /* out of memory error thrown */
printf ( "Java_ObjectArrayTest_initInt2DArray after
NewObjectArray\n" ) ;
printf ( "size: %d\n", size ) ;
for ( i = 0 ; i < size ; i++ )
{
printf ( "i: %d\n", i ) ;
jint tmp [ 256 ] ; /* make sure it is large enough */
int j ;
printf ( "before NewIntArray\n" ) ;
jintArray iarr = (*env)->NewIntArray ( env, size ) ;
if ( iarr == NULL )
return NULL ;
printf ( "after NewIntArray\n" ) ;
for ( j = 0 ; i < size ; i++ )
tmp [ j ] = i + j ;
printf ( "before SetIntArrayRegion\n" ) ;
(*env)->SetIntArrayRegion ( env, iarr, 0, size, tmp ) ;
printf ( "before SetObjectArrayElement\n" ) ;
(*env)->SetObjectArrayElement ( env, result, i, iarr ) ;
printf ( "before DeleteLocalRef\n" ) ;
(*env)->DeleteLocalRef ( env, iarr ) ;
printf ( "after DeleteLocalRef\n" ) ;
}
return result ;
}
more ObjectArrayTest.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class ObjectArrayTest */
#ifndef _Included_ObjectArrayTest
#define _Included_ObjectArrayTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ObjectArrayTest
* Method: initInt2DArray
* Signature: (I)[[I
*/
JNIEXPORT jobjectArray JNICALL Java_ObjectArrayTest_initInt2DArray
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif