java初步jni学习

来源:互联网 发布:java项目原代码 编辑:程序博客网 时间:2024/05/16 05:56
java文件:public class test {public static void main(String args[]){test m=new test();System.out.println(m.hello(2, 3));}static{System.loadLibrary("hello");}public native int hello(int a,int b);}在工程的bin目录下新建run.bat:@echo offc:cd C:\Documents and Settings\xy\workspace\jni\binjavah -jni com.xy.jni.mainecho. & pause运行run.bat文件得到.h文件:/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_xy_jni_main */#ifndef _Included_com_xy_jni_main#define _Included_com_xy_jni_main#ifdef __cplusplusextern "C" {#endif/* * Class:     com_zte_jni_main * Method:    hello * Signature: (II)I */JNIEXPORT jint JNICALL _Java_com_xy_jni_test_hello  (JNIEnv *, jobject, jint, jint);#ifdef __cplusplus}#endif#endif然后新建一个文件夹,用于编写c代码:/* DO NOT EDIT THIS FILE - it is machine generated */#include "com_zte_jni_test.h"/* Header for class com_xy_jni_main */JNIEXPORT jint JNICALL _Java_com_xy_jni_test_hello  (JNIEnv *env, jobject obj, jint a, jint b){return a+b;}在目录下新建一个hello.def文件:EXPORTSJava_com.xy.jni.test在目录下新建一个Crun.bat:@echo offc:cd C:\Documents and Settings\xy\桌面\jnigcc -c -I"d:\java\include" -I"d:java\include\win32" -o jni.o com_xy_jni_main.cgcc -shared -o hello.dll jni.o hello.defecho. & pause默认编写.h文件Java_com_zte_jni_test_hello前面没有_下滑线的,运行使会报如下错误:java.lang.UnsatisfiedLinkError(加个下划线就可以哦.....)