package com.home;public class Testdll {// 聲明所調用的庫名稱static {System.loadLibrary("hello");}// 對將要調用的方法做本地聲明,native關鍵字不可少public native static int get();public native static void set(int i);public static void main(String[] args) {Testdll test = new Testdll();test.set(5);System.out.println(test.get());}}
程式調用來產生所需的庫檔案。
注意操作:首先進入src目錄下,然後執行
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_home_Testdll */#ifndef _Included_com_home_Testdll#define _Included_com_home_Testdll#ifdef __cplusplusextern "C" {#endif/* * Class: com_home_Testdll * Method: get * Signature: ()I */JNIEXPORT jint JNICALL Java_com_home_Testdll_get (JNIEnv *, jclass);/* * Class: com_home_Testdll * Method: set * Signature: (I)V */JNIEXPORT void JNICALL Java_com_home_Testdll_set (JNIEnv *, jclass, jint);#ifdef __cplusplus}#endif#endif
JNIEXPORT jint JNICALL Java_com_home_Testdll_get (JNIEnv *, jclass);
#include "com_home_Testdll.h" int i = 0; JNIEXPORT jint JNICALL Java_com_home_Testdll_get (JNIEnv *, jclass) { return i; } JNIEXPORT void JNICALL Java_com_home_Testdll_set (JNIEnv *, jclass, jint) { i = j; }