Java Code :
package Package;
public class IntArray {
private native int sumArray(int[] arr);
public static void main(String args[]) {
IntArray p = new IntArray();
int arr[] = new int[10];
for(int i=0 ; i<10 ; i++) {
arr[i] = i;
}
int sum = p.sumArray(arr); // pass the array to the c function sumArray
System.out.println("Sum is : " + sum);
}
static { // 17th statement
System.loadLibrary("IntArray");
}
}
C code :
#include<stdio.h>
#include "Package_IntArray.h"
jint Java_Package_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr)
{
jint buf[10];
jint i, sum = 0;
(*env)->GetIntArrayRegion(env, arr, 0, 10, buf);
for (i = 0; i < 10; i++) {
sum += buf[i];
}
return sum;
}
Then i run the command :
java -Djava.library.path=W:\elita\jnitesters\workspace\c\IntArray\bin\Debug Package.IntArray
to which i get the following exceptions :
Exception in thread "main" java.lang.UnsatisfiedLinkError: Package.IntArray.sumArray([I)I
at Package.IntArray.sumArray(Native Method)
at Package.IntArray.main(IntArray.java:17)
There is a dll file named IntArray.dll in the directory W:\elita\jnitesters\workspace\c\IntArray\bin\Debug
Everything is fine ! But there is one thing you should attempt. Permanently delete all sorts of files(including .dll file) created by the IDE code::blocks . And then rebuild your program. Omit MACROS if any ! Then it should work.