JNI/NDK Development Guide (2) -- JVM rules for finding java native Methods
Through the first article, we understand that before calling the native method, we must first call the System. the loadLibrary interface loads a dynamic library that implements the native Method for normal access. Otherwise, java is thrown. lang. the UnsatisfiedLinkError is abnormal and the XX method cannot be found. Now let's think about how the JVM correctly finds the native function implemented by C/C ++ in the dynamic library when calling a native method in Java?
There are two methods for JVM to find native:
1> naming rules in accordance with JNI specifications
2> call the RegisterNatives function provided by JNI to register the local function to JVM. (Details will be provided later)
This article introduces the first method in detail through Java_com_study_jnilearn_HelloWorld_sayHello function in the first article HelloWorld example:
/* DO NOT EDIT THIS FILE - it is machine generated */#include
/* Header for class com_study_jnilearn_HelloWorld */#ifndef _Included_com_study_jnilearn_HelloWorld#define _Included_com_study_jnilearn_HelloWorld#ifdef __cplusplusextern "C" {#endif/* * Class: com_study_jnilearn_HelloWorld * Method: sayHello * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_study_jnilearn_HelloWorld_sayHello (JNIEnv *, jclass, jstring);#ifdef __cplusplus}#endif#endif
Roles of JNIEXPORT and JNICALL:
In the previous article, when we compile HelloWorld. c into a dynamic library, we use the-I parameter to include two header file directories under the JDK installation directory:
gcc -dynamiclib -o /Users/yangxin/Library/Java/Extensions/libHelloWorld.jnilib jni/HelloWorld.c -framework JavaVM -I/$JAVA_HOME/include -I/$JAVA_HOME/include/darwin
The first directory is jni. directory where the h header file is located, and the second is the cross-platform header file directory (the directory under mac OS x is named darwin, And the directory under windows is named win32, And the directory under linux is named linux ), used to define platform-related macros,
Two macros are used to identify the purpose of the function. JNIEXPORTAnd JNICALL are defined under the darwin directoryJni_md.h Header file. Compiling dll dynamic libraries in Windows requires that if a function in the dynamic library is to be called externally, you must add the _ declspec (dllexport) identifier in the function declaration, this function can be called externally. In Linux/Unix systems, these two macros can be omitted without being added. The difference between the two platforms is that the formats of executable files generated by their compilers are different. Here is an article detailing the dynamic libraries compiled by the two platforms: http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html. The value of JNICALL in windows is _ stdcall, which is used to constrain the function's inbound stack sequence and stack clearing rules.
Content of the jni_md.h header file in Windows:
#ifndef _JAVASOFT_JNI_MD_H_#define _JAVASOFT_JNI_MD_H_#define JNIEXPORT __declspec(dllexport)#define JNIIMPORT __declspec(dllimport)#define JNICALL __stdcalltypedef long jint;typedef __int64 jlong;typedef signed char jbyte;#endif
Content of the jni_md.h header file in Linux:
#ifndef _JAVASOFT_JNI_MD_H_#define _JAVASOFT_JNI_MD_H_#define JNIEXPORT#define JNIIMPORT#define JNICALLtypedef int jint;#ifdef _LP64 /* 64-bit Solaris */typedef long jlong;#elsetypedef long long jlong;#endiftypedef signed char jbyte;#endif
The jni_md.h header file in Linux shows that JNIEXPORT and JNICALL are empty definitions. Therefore, the JNI function declaration in Linux can omit these two macros.
Function naming rules:
Use the javah tool to generate the function prototype header file. The function naming rule is: Java _ class full path _ method name. For example, Java_com_study_jnilearn_HelloWorld_sayHello, where Java _ Is the function prefix, com_study_jnilearn_HelloWorld is the class name, sayHello is the method name, and they are connected by _ (underline.
Function parameters:
JNIEXPORT jstring JNICALL Java_com_study_jnilearn_HelloWorld_sayHello (JNIEnv *, jclass, jstring );
The first parameter: JNIEnv * is the first parameter for defining any native function (including the function registered by calling the RegisterNatives function of JNI). It is used to access the JNI function table pointer of the JVM data structure.
The second parameter is to call an instance or Class Object of the native method in java. If the native method is an instance method, this parameter is jobject. If the native method is a static method, it is jclass.
Third parameter: Java corresponds to the Data Type in JNI, and the String type in Java corresponds to the jstring type of JNI. (The ing between JAVA and JNI data types will be detailed later)
Function return value type:
The jstring clipped between the JNIEXPORT and JNICALL macro indicates the type of the return value of the function, corresponding to the String type of Java.
Summary:
After we are familiar with the native function naming rules of JNI, we can generate a function prototype for the corresponding java native method without using the javah command, you only need to compile the corresponding function prototype and implementation according to the function naming rules.
For example, com. study. jni. there is also an native instance method add for calculation addition in the Utils class, which has two int parameters and one int return value: public native int add (int num1, int num2 ), the prototype of the corresponding JNI function is JNIEXPORT jint JNICALL Java_com_study_jni_Utils_add (JNIEnv *, jobject, jint, jint );