ArrayJNI.java
1 class ArrayJNI{
2 public native int getSum(int[] numbers);
3
4 public static void main(String args[]){
5 System.loadLibrary("arraylib");
6 ArrayJNI arrjni=new ArrayJNI();
7 int intarr[]={12,32,43,13,54,5,-2};
8 int sum=arrjni.getSum(intarr);
9 System.out.println(sum);
10 }
11 }
ArrayJNI.h
1 /* DO NOT EDIT THIS FILE - it is machine generated */
2 #include <jni.h>
3 /* Header for class ArrayJNI */
4
5 #ifndef _Included_ArrayJNI
6 #define _Included_ArrayJNI
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 /*
11 * Class: ArrayJNI
12 * Method: getSum
13 * Signature: ([I)I
14 */
15 JNIEXPORT jint JNICALL Java_ArrayJNI_getSum
16 (JNIEnv *, jobject, jintArray);
17
18 #ifdef __cplusplus
19 }
20 #endif
21 #endif
ArrayJNI.cpp
1 #include "ArrayJNI.h"
2
3
4 JNIEXPORT jint JNICALL Java_ArrayJNI_getSum
5 (JNIEnv *env, jobject obj, jintArray array){
6 int i,sum=0;
7 jsize len=env->GetArrayLength(array);
8 jint *body=env->GetIntArrayElements(array,0);
9 for(i=0;i<len;i++){
10 sum+=body[i];
11 }
12 env->ReleaseIntArrayElements(array,body,0);
13 return sum;
14 }
Download source codes
0 comments:
Post a Comment