简单介绍及应用如下:
static { System.loadLibrary(“goodlUCk”); }
在这里,库的扩展名字可以不用写出来,究竟是DLL还是SO,由系统自己判定。
public class testdll { static { System.loadLibrary("goodluck"); } public native static int get(); public native static void set(int i); public static void main(String[] args) { testdll test = new testdll(); test.set(10); System.out.PRintln(test.get()); } }
对于已生成的.h头文件,C/C++所需要做的,就是把它的各个方法具体的实现。然后编译连接成库文件即可。再把库文件拷贝到JAVA程序的路径下面,就可以用JAVA调用C/C++所实现的功能了。
接上例子。我们先看一下testdll.h文件的内容:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class testdll */ #ifndef _Included_testdll #define _Included_testdll #ifdef __cplusplus extern "C" { #endif /* * Class: testdll * Method: get * Signature: ()I */ JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass); /* * Class: testdll * Method: set * Signature: (I)V */ JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint); #ifdef __cplusplus } #endif #endif
#include "testdll.h" int i = 0; JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass) { return i; } JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint j) { i = j; }
public class SendSMS { static { System.out.println(System.getProperty("java.library.path")); System.loadLibrary("sms"); } public native static int SmsInit(); public native static int SmsSend(byte[] mobileNo, byte[] smContent); }
/* DO NOT EDIT THIS FILE - it is machine generated */ #include /* Header for class com_mobilesoft_sms_mobilesoftinfo_SendSMS */ #ifndef _Included_com_mobilesoft_sms_mobilesoftinfo_SendSMS #define _Included_com_mobilesoft_sms_mobilesoftinfo_SendSMS #ifdef __cplusplus extern "C" { #endif /* * Class: com_mobilesoft_sms_mobilesoftinfo_SendSMS * Method: SmsInit * Signature: ()I */ JNIEXPORT jint JNICALL Java_com_mobilesoft_sms_mobilesoftinfo_SendSMS_SmsInit (JNIEnv *, jclass); /* * Class: com_mobilesoft_sms_mobilesoftinfo_SendSMS * Method: SmsSend * Signature: ([B[B)I */ JNIEXPORT jint JNICALL Java_com_mobilesoft_sms_mobilesoftinfo_SendSMS_SmsSend (JNIEnv *, jclass, jbyteArray, jbyteArray); #ifdef __cplusplus } #endif #endif
/* * SMS API * Author: yippit * Date: 2004.6.8 */ #ifndef MCS_SMS_H #define MCS_SMS_H #define DLLEXPORT __declspec(dllexport) /*sms storage*/ #define SMS_SIM 0 #define SMS_MT 1 /*sms states*/ #define SMS_UNREAD 0 #define SMS_READ 1 /*sms type*/ #define SMS_NOPARSE -1 #define SMS_NORMAL 0 #define SMS_Flash 1 #define SMS_MMSNOTI 2 typedef struct tagSmsEntry { int index; /*index, start from 1*/ int status; /*read, unread*/ int type; /*-1-can't parser 0-normal, 1-flash, 2-mms*/ int storage; /*SMS_SIM, SMS_MT*/ char date[24]; char number[32]; char text[144]; } SmsEntry; DLLEXPORT int SmsInit(void); DLLEXPORT int SmsSend(char *phonenum, char *content); DLLEXPORT int SmsSetSCA(char *sca); DLLEXPORT int SmsGetSCA(char *sca); DLLEXPORT int SmsSetInd(int ind); DLLEXPORT int SmsGetInd(void); DLLEXPORT int SmsGetInfo(int storage, int *max, int *used); DLLEXPORT int SmsSaveFlash(int flag); DLLEXPORT int SmsRead(SmsEntry *entry, int storage, int index); DLLEXPORT int SmsDelete(int storage, int index); DLLEXPORT int SmsModifyStatus(int storage, int index); /*unread -> read*/ #endif