Source: JNI Error Summary
Recently, the company has to use the JNI technology to use Java to invoke the already written local DLL library. Before I have not contacted the relevant technology, which spent most of the time debugging errors above, online for the wrong solution is not much, and now the project is nearing completion, oneself should also encounter some of the problems to be summarized.
1. Related commands:
Generates a JNI. h header file that contains the Java source code for the native function: javah-jni com.xxx.test
Note: Test is a compiled. class file to write the full path, which is performed in the upper directory of COM. The more complete command is: javah-classpath .-jni com.xxx.test Note the middle dot
After you write the. CPP code that implements the above. h file, build the DLL target file: g++ -wl,--kill-at-shared-o test1.dll test2.cpp
Note: Test1.dll for the generated target dll,test2 to the CPP source file. The red part is not small, which means generating a function without @ .
2. How to convert the string type in Java to the char* type in C:
Char* Jstringtostring (jnienv* env, jstring jstr)//Change type string into char*{ Char* Rtn =NULL; Jclass clsstring= Env->findclass ("java/lang/string"); Jstring Strencode= Env->newstringutf ("Utf-8"); Jmethodid Mid= Env->getmethodid (clsstring, "GetBytes", "(ljava/lang/string;) [B]); Jbytearray Barr= (Jbytearray) env->Callobjectmethod (Jstr, Mid, Strencode); Jsize Alen= env->Getarraylength (Barr); Jbyte* ba = env->getbytearrayelements (Barr, Jni_false); if(Alen > 0) {Rtn= (Char*) malloc (alen + 1); memcpy (RTN, BA, Alen); Rtn[alen]= 0; } env->releasebytearrayelements (Barr, BA, 0); returnRtn;}
3. How to convert the char* type in C to a string type in Java:
Const Char* PAT)//changetype char* into string{ = Env->findclass ("ljava/lang/ String; " ); = Env->getmethodid (strclass, "<init>", "([bljava/lang/string;) V"); = env->Newbytearray (strlen (PAT)); Env->setbytearrayregion (bytes, 0, strlen (PAT), (jbyte*) PAT); = Env->newstringutf ("Utf-8"); return (jstring) env->NewObject (strclass, Ctorid, Bytes, encoding);}
How to return the ArrayList type in 4.JNI:
Jclass cls_arraylist = Env->findclass ("Java/util/arraylist"); Jmethodid construct= Env->getmethodid (cls_arraylist, "<init>", "() V");//Note that the second parameter is <INIT>, and some information on the web is written as empty or <init></init> is wrong. Jobject obj_arraylist = Env->newobject (Cls_arraylist,construct, ""); Jmethodid Arraylist_add= Env->getmethodid (Cls_arraylist, "Add", "(Ljava/lang/object;) Z");//get ArrayList's Add () method//User ObjectJclass Cls_user = Env->findclass ("user");//Note that the user's path to complete, the name of the add-on place to add//None argument construct functionJmethodid Construct_user = Env->getmethodid (Cls_user, "<init>", "() V"); //new A objectJobject Obj_user = Env->newobject (Cls_user,construct_user, ""); //Get method ID /*Jmethodid User_setid = Env->getmethodid (Cls_user, "SetId", "(J) V"); Jmethodid user_setusername = Env->getmethodid (Cls_user, "Setusername", "(ljava/lang/string;) V"); Jmethodid User_setman = Env->getmethodid (Cls_user, "Setman", "(Z) V"); Jmethodid user_setage = Env->getmethodid (Cls_user, "Setage", "(I) V"); */ inti; for(i=0;i<10;i++){ //new A objectJobject Obj_user = Env->newobject (Cls_user,construct_user, ""); //get Field IDJfieldid user_id = Env->getfieldid (cls_user, "id", "J"); Jfieldid user_name= Env->getfieldid (Cls_user, "UserName", "ljava/lang/string;"); Jfieldid User_isman= Env->getfieldid (Cls_user, "Isman", "Z"); Jfieldid User_age= Env->getfieldid (Cls_user, "age", "I"); Env-Setlongfield (obj_user,user_id,i); Env->setobjectfield (Obj_user,user_name,env->newstringutf ("CC")); Env->setbooleanfield (obj_user,user_isman,1); Env->setintfield (obj_user,user_age,21); Env-Callobjectmethod (Obj_arraylist,arraylist_add,obj_user); } returnobj_arraylist;}
5. For encountering the following error:
# A Fatal error have been detected by the Java Runtime environment:## exception_access_violation (0xc0000005) at p c=0x7c92100b, pid=596, tid=20806.0_21-b07# Java vm:java HotSpot (TM) Client VM (17.0-b17 mixed mode, SHA Ring windows-x86) # problematic frame:# C [ntdll.dll+0x100b]## If you would like to submit a bug report, plea Se visit:# http://java.sun.com/webapps/bugreport/crash.jspnativefor where to report the bug.
This is usually due to a problem with the JNI-converted CPP Implementation code you wrote. Can focus on the Red section, if pc=0x00000000, it means that you want to implement the native function passed in a null pointer void* or NULL.
And if you pass in the parameters and structure variables, then the empty value here also to consider whether some members of the structure is also empty, thefunction in JNI is not allowed to pass in null value, this is not mentioned in the online information, I also stuck here for a long time. But you have to pass in the null value, but also to call the next layer of the DLL in the function, that will be an error, then what to do? You can then add a transition layer (CPP file) between them to implement the indirect call.
6. Finally note the release of resources in the JNI, if the use of too many resources are not released in time, it will be possible to report the above error.
such as: env->releasestringutfchars (xxx, XXX);
As for the other questions on the Internet are not written.
JNI Error Summary (RPM)