Java 代码:
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代码:
#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;
}
然后我运行命令:
java -Djava.library.path=W:\elita\jnitesters\workspace\c\IntArray\bin\Debug Package.IntArray
我得到以下例外:
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)
在目录W:\elita\jnitesters\workspace\c\IntArray\bin\Debug中有一个名为IntArray.dll的dll文件