正在阅读:Java: JNI完全手册Java: JNI完全手册

2005-06-17 10:20 出处: 作者:yippit 责任编辑:moningfeng
我的项目比较复杂,需要调用动态链接库,这样在JNI传送参数到C程序时,需要对参数进行处理转换。才可以被C程序识别。   大体程序如下:

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); }

  在这里要注意的是,path里一定要包含类库的路径,否则在程序运行时会抛出异常:   java.lang.UnsatisfiedLinkError: no sms in java.library.path   at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491)   at java.lang.Runtime.loadLibrary0(Runtime.java:788)   at java.lang.System.loadLibrary(System.java:834)   at com.mobilesoft.sms.mobilesoftinfo.SendSMS.(SendSMS.java:14)   at com.mobilesoft.sms.mobilesoftinfo.test.main(test.java:18)   Exception in thread "main"   指引的路径应该到.dll文件的上一级,如果指到.dll,则会报:   java.lang.UnsatisfiedLinkError: C:\sms.dll: Can't find dependent libraries   at java.lang.ClassLoader$NativeLibrary.load(Native Method)   at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1560)   at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1485)   at java.lang.Runtime.loadLibrary0(Runtime.java:788)   at java.lang.System.loadLibrary(System.java:834)   at com.mobilesoft.sms.mobilesoftinfo.test.main(test.java:18)   Exception in thread "main"   通过编译,生成com_mobilesoft_sms_mobilesoftinfo_SendSMS.h头文件。(建议使用Jbuilder进行编译,操作比较简单!)这个头文件就是Java和C之间的纽带。要特别注意的是方法中传递的参数jbyteArray,这在接下来的过程中会重点介绍。

/* 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

  对于我要调用的C程序的动态链接库,C程序也要提供一个头文件,sms.h。这个文件将要调用的方法罗列了出来。

/* * 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

键盘也能翻页,试试“← →”键

关注我们

最新资讯离线随时看 聊天吐槽赢奖品