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 ArrayIndexOutOf BoundsException .
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 buildObjectArra yTest.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 libObjectArrayT est.so
java -Djava.library.p ath=. ObjectArrayTest
Java_ObjectArra yTest_initInt2D Array entered
Java_ObjectArra yTest_initInt2D Array after FindClass
Java_ObjectArra yTest_initInt2D Array before NewObjectArray
Java_ObjectArra yTest_initInt2D Array after NewObjectArray
size: 3
i: 0
before NewIntArray
after NewIntArray
before SetIntArrayRegi on
before SetObjectArrayE lement
before DeleteLocalRef
after DeleteLocalRef
Exception in thread "main" java.lang.Array IndexOutOfBound sException
at ObjectArrayTest .initInt2DArray (Native Method)
at ObjectArrayTest .main(ObjectArr ayTest.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.prin t ( " " + i2arr[i][j] ) ;
}
System.out.prin tln () ;
}
}
static
{
System.loadLibr ary ( "ObjectArrayTes t" ) ;
}
}
#include <jni.h>
#include <stdio.h>
#include "ObjectArrayTes t.h"
/*
* Class: ObjectArrayTest
* Method: initInt2DArray
* Signature: (I)[[I
*/
JNIEXPORT jobjectArray JNICALL
Java_ObjectArra yTest_initInt2D Array ( JNIEnv *env, jclass cls, int size )
{
jobjectArray result ;
int i ;
printf ( "Java_ObjectArr ayTest_initInt2 DArray entered\n" ) ;
jclass intArrCls = (*env)->FindClass ( env, "[I" ) ;
printf ( "Java_ObjectArr ayTest_initInt2 DArray after
FindClass\n" ) ;
if ( intArrCls == NULL )
return NULL ; /* exception thrown */
printf ( "Java_ObjectArr ayTest_initInt2 DArray before
NewObjectArray\ n" ) ;
result = (*env)->NewObjectArr ay ( env, size, intArrCls, NULL ) ;
if ( result == NULL )
return NULL ; /* out of memory error thrown */
printf ( "Java_ObjectArr ayTest_initInt2 DArray 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 SetIntArrayRegi on\n" ) ;
(*env)->SetIntArrayReg ion ( env, iarr, 0, size, tmp ) ;
printf ( "before SetObjectArrayE lement\n" ) ;
(*env)->SetObjectArray Element ( env, result, i, iarr ) ;
printf ( "before DeleteLocalRef\ n" ) ;
(*env)->DeleteLocalR ef ( 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_Objec tArrayTest
#define _Included_Objec tArrayTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ObjectArrayTest
* Method: initInt2DArray
* Signature: (I)[[I
*/
JNIEXPORT jobjectArray JNICALL Java_ObjectArra yTest_initInt2D Array
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
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 ArrayIndexOutOf BoundsException .
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 buildObjectArra yTest.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 libObjectArrayT est.so
java -Djava.library.p ath=. ObjectArrayTest
Java_ObjectArra yTest_initInt2D Array entered
Java_ObjectArra yTest_initInt2D Array after FindClass
Java_ObjectArra yTest_initInt2D Array before NewObjectArray
Java_ObjectArra yTest_initInt2D Array after NewObjectArray
size: 3
i: 0
before NewIntArray
after NewIntArray
before SetIntArrayRegi on
before SetObjectArrayE lement
before DeleteLocalRef
after DeleteLocalRef
Exception in thread "main" java.lang.Array IndexOutOfBound sException
at ObjectArrayTest .initInt2DArray (Native Method)
at ObjectArrayTest .main(ObjectArr ayTest.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.prin t ( " " + i2arr[i][j] ) ;
}
System.out.prin tln () ;
}
}
static
{
System.loadLibr ary ( "ObjectArrayTes t" ) ;
}
}
#include <jni.h>
#include <stdio.h>
#include "ObjectArrayTes t.h"
/*
* Class: ObjectArrayTest
* Method: initInt2DArray
* Signature: (I)[[I
*/
JNIEXPORT jobjectArray JNICALL
Java_ObjectArra yTest_initInt2D Array ( JNIEnv *env, jclass cls, int size )
{
jobjectArray result ;
int i ;
printf ( "Java_ObjectArr ayTest_initInt2 DArray entered\n" ) ;
jclass intArrCls = (*env)->FindClass ( env, "[I" ) ;
printf ( "Java_ObjectArr ayTest_initInt2 DArray after
FindClass\n" ) ;
if ( intArrCls == NULL )
return NULL ; /* exception thrown */
printf ( "Java_ObjectArr ayTest_initInt2 DArray before
NewObjectArray\ n" ) ;
result = (*env)->NewObjectArr ay ( env, size, intArrCls, NULL ) ;
if ( result == NULL )
return NULL ; /* out of memory error thrown */
printf ( "Java_ObjectArr ayTest_initInt2 DArray 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 SetIntArrayRegi on\n" ) ;
(*env)->SetIntArrayReg ion ( env, iarr, 0, size, tmp ) ;
printf ( "before SetObjectArrayE lement\n" ) ;
(*env)->SetObjectArray Element ( env, result, i, iarr ) ;
printf ( "before DeleteLocalRef\ n" ) ;
(*env)->DeleteLocalR ef ( 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_Objec tArrayTest
#define _Included_Objec tArrayTest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ObjectArrayTest
* Method: initInt2DArray
* Signature: (I)[[I
*/
JNIEXPORT jobjectArray JNICALL Java_ObjectArra yTest_initInt2D Array
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
Comment