JNI Complete Manual (2)

zhaozj2021-02-12  152

JNI Complete Manual (2)

From Yippit

My project is more complicated and needs to call dynamic link libraries so that the parameters are required to process the parameters when the JNI transmits parameters to the C program. Can be identified by the C program. The general procedure is as follows: public class sendsms {static {system.out.println ("java.library.path")); System.LoadLibrary ("SMS");} public native static int smsinit (); public native Static int smssend (byte [] mobileno, byte [] smcontent;} At this point here, it is necessary to include the path to the class library, otherwise it will throw an exception when the program is run: java.lang.unsatisfiedLinkerRor: no sms in java.library.pathat 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.Smsms. (sendsms.java: 14) at com.mobilesoft.sms.MobilesoftInfo.test.main (Test.java: 18) Exception in thread "main" Path The last level of the .dll file, if you refer to .dll, the report: java.lang.unsatisfiedlinkerror: c: /sms.dll: can't find dependent librariesat 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.loadli Brary (System.java: 834) at com.mobilesoft.sms.mobilesoftinfo.Smendsms. (sendsms.java: 14) at com.mobilesoft.sms.MobilesoftInfo.test.main (Test.java: 18) Exception in Thread "main" generates COM_MOBILESOFT_SMS_MOBILESOFTINFO_SENDSMS.H header file. (It is recommended to use JBuilder to compile, the operation is relatively simple!) This header is the link between Java and C. Special attention is to note that the parameters transmitted in the method JByteaRray, which will focus on the next process.

/ * 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 __cplusplusextern "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; # neydef __cplusplus} # Endif # Endif For the dynamic link library of the C program I want to call, the C program also provides a header file, SMS.H. This file will be called by the method.

/ ** 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 2typedef 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, INDEX); DLLEXPORT INT SMSDELETE (Int Storage, INDEX); DLLEXPORT INT SMSMODIFYSTATUS (INT Storage, int index; / * unread -> read * / # Endif After having the two header files, you can write a C program. That is, two methods for achieving JNI calls. In the online information, since the method of calling is relatively simple, (mostly a print string, etc.), so the most troublesome part of the JNI is avoided, and the most critical part, the passage of the parameters. Since Java and C coding is different, the passable parameters are to be processed, otherwise the C program will warn the parameters during the compilation process, for example; Warning C4024: 'smssend': Different Types for Formal and Actual Parameter 2, etc.

Sms.c procedure is as follows: #include "sms.h" #include "com_mobilesoft_sms_mobilesoftinfo_SendSMS.h" JNIEXPORT jint JNICALL Java_com_mobilesoft_sms_mobilesoftinfo_SendSMS_SmsInit (JNIEnv * env, jclass jobject) {return SmsInit ();} JNIEXPORT jint JNICALL Java_com_mobilesoft_sms_mobilesoftinfo_SendSMS_SmsSend (JNIEnv * env, jclass jobject, jbyteArray mobileno, jbyteArray smscontent) {char * pSmscontent; // jsize theArrayLengthJ = (* env) -> GetArrayLength (env, mobileno); jbyte * arrayBody = (* env) -> GetByteArrayElements (env, mobileno, 0); Char * Pmobileno = (char *) ArrayBody; Printf ("[% s] / n", pmobileno; // jsize size = (* ENV) -> GetArrayLength (ENV, SMSCONTENT); arraybody = (* ENV) -> GetByteaRrayElements (ENV, SMSCONTENT, 0); psmscontent = (char *) ArrayBody; Printf ("<% s> / n", psmscontent; returno, psmscontent);} For C or C , it is meeting It is slightly different, which can be appropriately modified by the reader. It should be noted here that the methods already included in these JNIs such as GetArrayLength, GetByteaRayelements, which are specifically provided to the transition parameter type. There are a lot of specific methods that will be specifically introduced in the next one. After completing the above file, you can compile SMS.C, generate .dll files (recommended to compile in Release, so that the volume of the dynamic link library will be smaller!) After completing the .dll file, you can in Java The method in the C procedure is used.

转载请注明原文地址:https://www.9cbs.com/read-7256.html

New Post(0)