Original URL: http://leidiqiu.iteye.com/blog/720307
The basic problem of JNI is to solve the communication problem that the Java and C + + code call each other, the biggest problem in the writing process of C + + code is to adapt to the code writing rules, C + + call or return content must obey the rules of communication between JVM and C + + code.
The general procedure for calling Java in C + + is as follows:
- Get class:
- Jclass cls = Env->findclass ("com/ldq/student");
- The CLS can be considered a handle to a class
- "Com/ldq/student" is a class file, note that "com.ldq.Student" cannot be used
- How to obtain:
- Jmethodid mid = Env->getmethodid (CLS, "<init>", "() V");
- The above is a constructor, the parameter is "<init>" "() V"
- Jmethodid mid = Env->getmethodid (CLS, "Getage", "() I");
- The above is the method of the class, the first argument is the class handle, the second argument is the method name, the third parameter is the signature identity
| java type |
symbol |
| boolean | td>z
| Byte |
b |
| Char |
C&NBSP;&N BSP; |
| short |
s |
| int |
i |
| long |
l |
| float |
f |
| Double |
d |
| Void |
v |
| Objects Object |
lfully-qualified-class-name; L class name; |
| Arrays array |
[array-type [Array type |
| Methods Method &nb sp; |
(Argument-types) Return-type (parameter type) return type |
- Get the object:
- Jobject Obj=env->newobject (Cls,mid);
- The above gets the handle of an object
- To get an object member variable:
- Jfieldid Fid=env->getfieldid (CLS, "Age", "I");
- The above and the obtained class method are almost
- Action member Variable:
- Jint A=env->getintfield (Obj,mid);
- age=age+10;
- Env->setintfield (Obj,fid,a);
- Return:
Here is an example of my practice
Java code
- Package com.ldq.list;
- Import java.util.List;
- public class Exlist {
- /**
- * @param args
- */
- public static void Main (string[] args) {
- TODO auto-generated Method Stub
- System.out.println ("-------wifimanager.test ()");
- System.out.println (Wifimanager.test ());
- System.out.println ("-------Wifimanager.testarray ()");
- string[] S1 = Wifimanager.testarray ();
- for (int i = 0; i < s1.length; i++) {
- System.out.println (S1[i]);
- }
- System.out.println ("-------Wifimanager.testobject ()");
- System.out.println (Wifimanager.testobject (). SSID);
- System.out.println (Wifimanager.testobject (). Mac);
- System.out.println (Wifimanager.testobject (). level);
- System.out.println ("-------Wifimanager.getscanresultsa ()");
- scanresult[] s2 = Wifimanager.getscanresultsa ();
- for (int i = 0; i < s2.length; i++) {
- System.out.println (S2[I].SSID);
- System.out.println (S2[I].MAC);
- System.out.println (S2[i].level);
- }
- System.out.println ("-------wifimanager.getscanresults ()");
- list<scanresult> list = Wifimanager.getscanresults ();
- System.out.println (List.get (0). SSID);
- System.out.println (List.get (0). Mac);
- System.out.println (List.get (0). level);
- }
- }
Java code
- Package com.ldq.list;
- public class Scanresult {
- String SSID;
- String mac;
- int level;
- Public Scanresult () {
- }
- Public Scanresult (String SSID, String Mac, int. level) {
- This.ssid = SSID;
- This.mac = Mac;
- This.level = level;
- }
- }
Java code
- Package com.ldq.list;
- Import java.util.List;
- public class Wifimanager {
- static {
- System.loadlibrary ("WiFi");
- }
- Public native static String test ();
- Public native static string[] Testarray ();
- Public native static Scanresult Testobject ();
- Public native static scanresult[] Getscanresultsa ();
- Public native static list<scanresult> getscanresults ();
- }
CPP Code
- #include <jni.h>
- #include "Com_ldq_list_wifimanager.h"
- /*
- * Class:com_ldq_list_wifimanager
- * Method:test
- * Signature: () ljava/lang/string;
- */
- Jniexport jstring jnicall java_com_ldq_list_wifimanager_test (jnienv *env, Jclass CLS)
- {
- return Env->newstringutf ("Hello");
- }
- /*
- * Class:com_ldq_list_wifimanager
- * Method:testarray
- * Signature: () [ljava/lang/string;
- */
- Jniexport jobjectarray jnicall Java_com_ldq_list_wifimanager_testarray (jnienv *env, Jclass CLS)
- {
- Jobjectarray ret;
- int i;
- Char *message[5]= {"First",
- "Second",
- "Third",
- "Fourth",
- "Fifth"};
- ret= (Jobjectarray) Env->newobjectarray (5,
- Env->findclass ("java/lang/string"),
- Env->newstringutf (""));
- for (i=0;i<5;i++) {
- Env->setobjectarrayelement (
- Ret,i,env->newstringutf (Message[i]));
- }
- return (ret);
- }
- /*
- * Class:com_ldq_list_wifimanager
- * Method:testobject
- * Signature: () Lcom/ldq/list/scanresult;
- */
- Jniexport jobject jnicall java_com_ldq_list_wifimanager_testobject (jnienv *env, Jclass CLS)
- {
- Jclass m_cls = Env->findclass ("Com/ldq/list/scanresult");
- Jmethodid mid = Env->getmethodid (M_cls, "<init>", "() V");
- Jobject obj = Env->newobject (m_cls,mid);
- Jfieldid Fid_ssid = Env->getfieldid (M_cls, "SSID", "ljava/lang/string;");
- Jfieldid Fid_mac = Env->getfieldid (M_cls, "Mac", "ljava/lang/string;");
- Jfieldid fid_level = Env->getfieldid (M_cls, "level", "I");
- Env->setobjectfield (Obj,fid_ssid,env->newstringutf ("AP1"));
- Env->setobjectfield (Obj,fid_mac,env->newstringutf ("00-11-22-33-44-55"));
- Env->setintfield (obj,fid_level,-66);
- return obj;
- }
- /*
- * Class:com_ldq_list_wifimanager
- * Method:getscanresultsa
- * Signature: () [Lcom/ldq/list/scanresult;
- */
- Jniexport jobjectarray jnicall Java_com_ldq_list_wifimanager_getscanresultsa (jnienv *env, Jclass CLS)
- {
- Jclass Cls_array=env->findclass ("Java/lang/object");
- Jobjectarray Obj_array=env->newobjectarray (2,cls_array,0);
- Jclass cls_obj = Env->findclass ("Com/ldq/list/scanresult");
- Jmethodid m = Env->getmethodid (Cls_obj, "<init>", "() V");
- Jfieldid Fid_ssid = Env->getfieldid (Cls_obj, "SSID", "ljava/lang/string;");
- Jfieldid Fid_mac = Env->getfieldid (Cls_obj, "Mac", "ljava/lang/string;");
- Jfieldid fid_level = Env->getfieldid (Cls_obj, "level", "I");
- for (int i=0;i<2;i++)
- {
- Jobject Obj=env->newobject (cls_obj,m);
- Jobject O1=env->newstringutf ("AP2");
- Env->setobjectfield (OBJ,FID_SSID,O1);
- Jobject O2=env->newstringutf ("22-22-22-22-22-22");
- Env->setobjectfield (OBJ,FID_MAC,O2);
- Env->setintfield (obj,fid_level,-66);
- Env->setobjectarrayelement (Obj_array,i,obj);
- }
- return obj_array;
- }
- /*
- * Class:com_ldq_list_wifimanager
- * Method:getscanresults
- * Signature: () ljava/util/list;
- */
- Jniexport jobject jnicall java_com_ldq_list_wifimanager_getscanresults (jnienv *env, Jclass CLS)
- {
- Jclass m_cls_list = Env->findclass ("Java/util/arraylist");
- Jmethodid m_mid_list = Env->getmethodid (m_cls_list, "<init>", "() V");
- Jobject m_obj_list = Env->newobject (m_cls_list,m_mid_list);
- Jmethodid M_mid_add = Env->getmethodid (m_cls_list, "Add", "(Ljava/lang/object;) Z");
- Jclass M_cls_result = Env->findclass ("Com/ldq/list/scanresult");
- Jmethodid M_mid_result = Env->getmethodid (M_cls_result, "<init>", "() V");
- Jobject M_obj_result = Env->newobject (M_cls_result,m_mid_result);
- Jfieldid m_fid_1 = Env->getfieldid (M_cls_result, "SSID", "ljava/lang/string;");
- Jfieldid m_fid_2 = Env->getfieldid (M_cls_result, "Mac", "ljava/lang/string;");
- Jfieldid m_fid_3 = Env->getfieldid (M_cls_result, "level", "I");
- Env->setobjectfield (M_obj_result,m_fid_1,env->newstringutf ("AP6"));
- Env->setobjectfield (M_obj_result,m_fid_2,env->newstringutf ("66-66-66-66-66-66"));
- Env->setintfield (m_obj_result,m_fid_3,-66);
- Env->callbooleanmethod (M_obj_list,m_mid_add,m_obj_result);
- return m_obj_list;
- }
"Go" JNI object processing