Thursday, April 16, 2009

JNI Example

How to use JNI ?? Click here to find out.

NativeAdd.java
public class NativeAdd{
native int addNums(int a, int b);
static{
System.loadLibrary("Add");
}
public static void main(String args[]){
NativeAdd na = new NativeAdd();
System.out.println(""+na.addNums(1,2));
}
}

NativeAdd.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class NativeAdd */

#ifndef _Included_NativeAdd
#define _Included_NativeAdd
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: NativeAdd
* Method: addNums
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_NativeAdd_addNums
(JNIEnv *, jobject, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

NativeAdd.c
#include "jni.h"
#include "NativeAdd.h"
JNIEXPORT jint JNICALL Java_NativeAdd_addNums
(JNIEnv * jenv, jobject jobj, jint a, jint b){
int sum = 0;
sum = a + b;
return sum;
}

No comments:

Post a Comment