📄 java jni2.htm
字号:
google_color_bg = "EDF0F5";
google_color_link = "0000FF";
google_color_url = "008000";
google_color_text = "000000";
//--></SCRIPT>
<SCRIPT src="Java JNI2.files/show_ads" type=text/javascript>
</SCRIPT>
</TD></TR></TBODY></TABLE>
<DIV id=article>
<TABLE cellSpacing=0 cellPadding=0 width=575 border=0>
<TBODY>
<TR>
<TD height=3></TD></TR>
<TR>
<TD vAlign=bottom align=right><A
href="http://www.qqread.com/java/w120282060.html">上一页</A> <A
href="http://www.qqread.com/java/w120282060.html">1</A> <FONT
color=#ff0000>2</FONT> <A
href="http://www.qqread.com/java/w120282060_3.html">3</A> <A
href="http://www.qqread.com/java/w120282060_3.html">下一页</A> </TD></TR>
<TR>
<TD class=height170><FONT class=f14
id=zoom> <BR> 我的项目比较复杂,需要调用动态链接库,这样在JNI传送参数到C程序时,需要对参数进行处理转换。才可以被C程序识别。<BR> 大体程序如下:<BR>
<P class=code>public class SendSMS { <BR>static <BR>{ <BR>
<TABLE style="MARGIN: 10px 8px 3px 7px" cellSpacing=0 cellPadding=0
align=left border=0>
<TBODY>
<TR>
<TD><SPAN
id=ad_qqread_mid_big></SPAN></TD></TR></TBODY></TABLE>System.out.println(System.getProperty("java.library.path"));
<BR>System.loadLibrary("sms"); <BR>} <BR>public native static int
SmsInit(); <BR>public native static int SmsSend(byte[] mobileNo,
byte[] smContent);
<BR>}</P><BR><BR> 在这里要注意的是,path里一定要包含类库的路径,否则在程序运行时会抛出异常:<BR> java.lang.UnsatisfiedLinkError:
no sms in java.library.path<BR> at
java.lang.ClassLoader.loadLibrary(ClassLoader.java:1491)<BR> at
java.lang.Runtime.loadLibrary0(Runtime.java:788)<BR> at
java.lang.System.loadLibrary(System.java:834)<BR> at
com.mobilesoft.sms.mobilesoftinfo.SendSMS.<CLINIT>(SendSMS.java:14)<BR> at
com.mobilesoft.sms.mobilesoftinfo.test.main(test.java:18)<BR> Exception
in thread
"main"<BR><BR> 指引的路径应该到.dll文件的上一级,如果指到.dll,则会报:<BR> java.lang.UnsatisfiedLinkError:
C:\sms.dll: Can't find dependent libraries<BR> at
java.lang.ClassLoader$NativeLibrary.load(Native Method)<BR> at
java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1560)<BR> at
java.lang.ClassLoader.loadLibrary(ClassLoader.java:1485)<BR> at
java.lang.Runtime.loadLibrary0(Runtime.java:788)<BR> at
java.lang.System.loadLibrary(System.java:834)<BR> at
com.mobilesoft.sms.mobilesoftinfo.test.main(test.java:18)<BR> Exception
in thread
"main"<BR><BR> 通过编译,生成com_mobilesoft_sms_mobilesoftinfo_SendSMS.h头文件。(建议使用Jbuilder进行编译,操作比较简单!)这个头文件就是Java和C之间的纽带。要特别注意的是方法中传递的参数jbyteArray,这在接下来的过程中会重点介绍。<BR>
<P class=code>/* DO NOT EDIT THIS FILE - it is machine generated */
<BR>#include <JNI.H><BR>/* Header for class
com_mobilesoft_sms_mobilesoftinfo_SendSMS */ <BR>#ifndef
_Included_com_mobilesoft_sms_mobilesoftinfo_SendSMS <BR>#define
_Included_com_mobilesoft_sms_mobilesoftinfo_SendSMS <BR>#ifdef
__cplusplus <BR>extern "C" { <BR>#endif <BR>/* <BR>* Class:
com_mobilesoft_sms_mobilesoftinfo_SendSMS <BR>* Method: SmsInit
<BR>* Signature: ()I <BR>*/ <BR>JNIEXPORT jint JNICALL
Java_com_mobilesoft_sms_mobilesoftinfo_SendSMS_SmsInit <BR>(JNIEnv
*, jclass); <BR>/* <BR>* Class:
com_mobilesoft_sms_mobilesoftinfo_SendSMS <BR>* Method: SmsSend
<BR>* Signature: ([B[B)I <BR>*/ <BR>JNIEXPORT jint JNICALL
Java_com_mobilesoft_sms_mobilesoftinfo_SendSMS_SmsSend <BR>(JNIEnv
*, jclass, jbyteArray, jbyteArray); <BR>#ifdef __cplusplus <BR>}
<BR>#endif
<BR>#endif</P><BR><BR> 对于我要调用的C程序的动态链接库,C程序也要提供一个头文件,sms.h。这个文件将要调用的方法罗列了出来。<BR>
<P class=code>/* <BR>* SMS API <BR>* Author: yippit <BR>* Date:
2004.6.8 <BR>*/ <BR>#ifndef MCS_SMS_H <BR>#define MCS_SMS_H
<BR>#define DLLEXPORT __declspec(dllexport) <BR>/*sms storage*/
<BR>#define SMS_SIM 0 <BR>#define SMS_MT 1 <BR>/*sms states*/
<BR>#define SMS_UNREAD 0 <BR>#define SMS_READ 1 <BR>/*sms type*/
<BR>#define SMS_NOPARSE -1 <BR>#define SMS_NORMAL 0 <BR>#define
SMS_FLASH 1 <BR>#define SMS_MMSNOTI 2 <BR>typedef struct tagSmsEntry
{ <BR>int index; /*index, start from 1*/ <BR>int status; /*read,
unread*/ <BR>int type; /*-1-can't parser 0-normal, 1-flash, 2-mms*/
<BR>int storage; /*SMS_SIM, SMS_MT*/ <BR>char date[24]; <BR>char
number[32]; <BR>char text[144]; <BR>} SmsEntry; <BR>DLLEXPORT int
SmsInit(void); <BR>DLLEXPORT int SmsSend(char *phonenum, char
*content); <BR>DLLEXPORT int SmsSetSCA(char *sca); <BR>DLLEXPORT int
SmsGetSCA(char *sca); <BR>DLLEXPORT int SmsSetInd(int ind);
<BR>DLLEXPORT int SmsGetInd(void); <BR>DLLEXPORT int SmsGetInfo(int
storage, int *max, int *used); <BR>DLLEXPORT int SmsSaveFlash(int
flag); <BR>DLLEXPORT int SmsRead(SmsEntry *entry, int storage, int
index); <BR>DLLEXPORT int SmsDelete(int storage, int index);
<BR>DLLEXPORT int SmsModifyStatus(int storage, int index); /*unread
-> read*/ <BR>#endif</P><BR><BR
clear=all></FONT></TD></TR></TBODY></TABLE></DIV>
<TABLE cellSpacing=0 cellPadding=0 width=575 border=0>
<TBODY>
<TR>
<TD height=10></TD></TR>
<TR>
<TD align=right><A
href="http://www.qqread.com/java/w120282060.html">上一页</A> <A
href="http://www.qqread.com/java/w120282060.html">1</A> <FONT
color=#ff0000>2</FONT> <A
href="http://www.qqread.com/java/w120282060_3.html">3</A> <A
href="http://www.qqread.com/java/w120282060_3.html">下一页</A> </TD></TR>
<TR>
<TD> </TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=575 border=0>
<TBODY>
<TR>
<TD align=right colSpan=2 height=19>【<A title="Java: JNI完全手册"
style="CURSOR: hand"
onclick="window.external.addFavorite(document.location.href,'Java: JNI完全手册')"
?>收藏此页</A>】【<A href="javascript:doZoom(16)">大</A> <A
href="javascript:doZoom(14)">中</A> <A
href="javascript:doZoom(12)">小</A>】【<A
href="javascript:doPrint()">打印</A>】【<A
href="javascript:window.close()">关闭</A>】 </TD></TR>
<TR>
<TD height=50><SPAN class=channel>上一篇:</SPAN><A
title="自己动手 让组装机也来一键恢复"
href="http://www.qqread.com/skill/h110660000.html"
target=_blank>自己动手 让组装机也来一键恢复</A><BR><SPAN
class=channel>下一篇:</SPAN><A title=定期更新密码才能够永保安全问答
href="http://www.qqread.com/skill/a601794001.html"
target=_blank>定期更新密码才能够永保安全问答</A><BR></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=575 border=0>
<TBODY>
<TR>
<TD align=middle height=15><SPAN id=google_ad_468x60></SPAN></TD></TR>
<TR>
<TD bgColor=#c6c9d1 height=1></TD></TR>
<TR>
<TD height=10></TD></TR></TBODY></TABLE>
<TABLE class=headframe cellSpacing=1 cellPadding=3 width=572 border=0>
<TBODY>
<TR>
<TD align=middle><IFRAME marginWidth=0 marginHeight=0
src="Java JNI2.files/picFrame_h.htm" frameBorder=0 width=558
scrolling=no height=135></IFRAME></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=1 width=575 border=0>
<TBODY>
<TR>
<TD width=285>
<TABLE class=bottomframe cellSpacing=1 cellPadding=3 width="100%"
border=0>
<TBODY>
<TR>
<TD class=title_rellink>热门相关链接</TD></TR>
<TR>
<TD class=height130>·<A title=java程序-JPTXXXXXXX1.91
href="http://www.qqread.com/encrypt/c400190062.html"
target=_blank>java程序-JPTXXXXXXX1.91</A><BR>·<A
title=Apple释放修复Java隐患补丁
href="http://www.qqread.com/news/n559103001.html"
target=_blank>Apple释放修复Java隐患补丁</A><BR>·<A
title="我的Thinking in Java学习笔记"
href="http://www.qqread.com/itlife/y712161000.html"
target=_blank>我的Thinking in Java学习笔记</A><BR>·<A
title=Java高手的感叹:走向J2EE,漫长的道路
href="http://www.qqread.com/itlife/w812161000.html"
target=_blank>Java高手的感叹:走向J2EE,漫长的道路</A><BR>·<A title=我的Java之路
href="http://www.qqread.com/itlife/y412162000.html"
target=_blank>我的Java之路</A><BR>·<A title="路漫漫其修远兮 Java的发展路在何方"
href="http://www.qqread.com/itlife/m212167000.html"
target=_blank>路漫漫其修远兮 Java的发展路在何方</A><BR>·<A
title=小议学习Java的浮躁心态
href="http://www.qqread.com/itlife/a512167000.html"
target=_blank>小议学习Java的浮躁心态</A><BR>·<A title=教你如何成为一名Java初级程序员
href="http://www.qqread.com/itlife/h712700000.html"
target=_blank>教你如何成为一名Java初级程序员</A><BR>·<A
title=追MM与Java的23种设计模式
href="http://www.qqread.com/itlife/h712881000.html"
target=_blank>追MM与Java的23种设计模式</A><BR>·<A
title="Microsoft和Sun角斗结束 Java将进入Wiondows XP"
href="http://www.qqread.com/itlife/z791145002.html"
target=_blank>Microsoft和Sun角斗结束
Java将进入W</A><BR></TD></TR></TBODY></TABLE></TD>
<TD width=5> </TD>
<TD width=285>
<TABLE class=bottomframe cellSpacing=1 cellPadding=3 width="100%"
border=0>
<TBODY>
<TR>
<TD class=title_rellink>最新相关链接</TD></TR>
<TR>
<TD class=height130>·<A
title="连接PHP和 Java -- PHP/Java Bridge [2]"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -