JavaSE JNI Dynamic Registration local method (native layer in C)

Source: Internet
Author: User

Introduction: I recently studied JNI in combination with the Android source code and found that the local JNI method binding on Android does not use static binding by function name, instead, it uses unusual dynamic binding. Therefore, the local JNI method is dynamically bound in JavaSE, and some problems occur more or less during implementation, most of the articles found on the Internet are just a Summary of the Android source code, but are not implemented in JavaSE. Therefore, this article fills in this blank. This article does not cover the basis of JNI. It is just an explanation of dynamic binding of local methods. If you don't understand why JNI is a theme, please Google to study JNI ~~ Dynamic Registration of Local methods requires several data structures and methods (in jni. h): [c] // This structure indicates the typeing between a local Java method and the c method on the native layer. typedef struct {char * name; // Java local method name char * signature; // Java local method signature void * fnPtr; // c function pointer} JNINativeMethod; // this method is called immediately after JNI dynamic library is loaded by JVM. Therefore, you can dynamically register JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM * vm, void * reserved) in this function ); // This method is used for Dynamic Registration // clazz: class of the local method // methods: struct array indicating the method ing relationship // nMethods: the size of the array jint (JNICALL * Reg IsterNatives) (JNIEnv * env, jclass clazz, const JNINativeMethod * methods, jint nMethods); first, let's take a look at the Declaration and Java code for calling the local method: [java] public class test {static {// load the JNI dynamic library System. loadLibrary ("test_native");} public static native String nativeFunc (String msg); public static void main (String [] args) {System. out. println (nativeFunc ("Hello Native") ;}} declare a native method nativeFunc in this Code. The function of this method is to add the input string to "(Sovle D) "and then return the modified string. Then let's take a look at the JNI dynamic link library code (implemented in c): [c] # include <stdio. h> # include <string. h> # include <jni. h> # include "test. h "jstring native_function (JNIEnv * env, jclass cls, jstring msg ); // define the ing between local Java methods and methods in the JNI dynamic library static JNINativeMethod gMethods [] ={{ "nativeFunc", "(Ljava/lang/String ;) ljava/lang/String; ", (void *) native_function}; // register the local method static int register_native_methods (JNIEnv * env, const char * clsname, co Nst JNINativeMethod * gMethods, int nums) {jclass clazz = (* env)-> FindClass (env, clsname); if (clazz = NULL) return JNI_FALSE; if (* env)-> RegisterNatives (env, clazz, gMethods, nums) <0) return JNI_FALSE; return JNI_TRUE;} JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM * vm, void * reserved) {JNIEnv * env = NULL; jint result =-1; // obtain JNIEnv if (* vm)-> GetEnv (vm, (void **) & env, JNI_VERSION_1_4 )! = JNI_ OK) return JNI_FALSE; // register the local method if (register_native_methods (env, "test", gMethods, sizeof (gMethods)/sizeof (gMethods [0])! = JNI_TRUE) return JNI_FALSE; result = JNI_VERSION_1_4; return result;} // custom method. Add "(Solved)" to msg and return jstring native_function (JNIEnv * env, jclass cls, jstring msg) {const char * cmsg; char cres [1024]; jstring res; cmsg = (* env)-> GetStringUTFChars (env, msg, JNI_FALSE); memset (cres, '\ 0', 1024); strcat (cres, cmsg); strcat (cres, "(Solved)"); res = (* env)-> NewStringUTF (env, cres); (* env)-> ReleaseStrin GUTFChars (env, msg, cmsg); return res;} pay attention to the operations in the JNI_OnLoad method. In the Android source code, the code for obtaining JNIEnv is vm-> GetEnv (void **) & env, JNI_VERSION_1_4). If this write is incorrect in our program, the request for member 'getenv' in something not a structure or union (or the request member 'getenv' in a non-structure or union request) error may occur, this is because the implementation of the native layer in the Android source code uses C ++, and here we use C language, this is also a problem found in many articles on the Internet. The final problem is the compilation and running of the Code: 1. compile and generate the dynamic link library libtest_native.so: gcc-fPIC-I $ JAVA_HOME/include- I $ JAVA_HOME/include/linux-shared-o libtest_native.so test. c here, the gcc option-fPIC-shared is used to generate a dynamic link library. The PIC option is used to generate a Position Independent Code, that is, to generate a machine Code that does not depend on a specific address, please see: http://stackoverflow.com/questions/5311515/gcc-fpic-option2. compile Java code and run javac test. java-Djava. library. path =. note:-Djava must be added when you run the Java program. library. path =. otherwise, the Java program cannot find the previously generated dynamic link library.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.