JNI static registration and dynamic registration Comments

JNI registration refers to a method java layer (modified native keyword) and a C layer association method, in order to achieve the object layer code calls layer c java code. JNI registration is divided into static registration and dynamic registration are two kinds of static registration is related through the fixed format method names, dynamic registration is to be associated by adding dynamic mapping between a method name can easily play, more flexible, we recommend using dynamic registration. Before proceeding registration, you need to download two Clion tools and eclipse (write java application can be), then we can start the registration.

Static registration

1. First, in eclipse create a new Java Application, name can be arbitrary, such as call JavaJni, then create a new package in the src directory named clz, then clz package under the new java class Register.java, class write a native method is as follows:

2. Go to the command line, came under Register.java directory, use the command javac Register.java generate Register.class file
3. Command line, back to the src directory, the command javah clz.Register generate clz_Register.h
4. In Clion, create a C ++ Library, Library type selected shared, jni.h jni_md.h files and the files and jdk / include copied
5. the third step is to copy the file to clz_Register.h generated in just the new Clion project
references 6. modify jni.h as follows:

  1. New file clz_Register.c introduced clz_Register.h, to achieve a corresponding function .h

  1. Add build configurations in Clion project in CMakeLists.txt

  • The first parameter firstlib, indicate the name of the dynamic library generated after compilation
  • The second argument can choose STATIC or SHARED, respectively, is a static library or a dynamic library, we generally use a dynamic library
  • The third and subsequent parameters, expressed the need to compile a file storage

9. Build-BuildProject selected in Clion may be generated at cmake-build-debug libfirstlib.dylib (mac as dylib, windows as DLL)
10. The generation of the step 8 libfirstlib.dylib eclipse libs directory to copy the item (no new)
11. Register.java loaded library, and the library function calls

Dynamic registration

  1. First, create a new eclipse Java Application, name can be arbitrary, such as call JavaJni, then create a new package in the src directory named register, the new java class DynamicRegister.java in the register package, class, write native methods are as follows:

  1. In Clion, create a C ++ Library, Library type selected shared, jni.h jni_md.h files and the files and jdk / include copied
  2. DynamicRegister.c new file, and the introduction jni.h implement two methods, as follows

  1. Add dynamic registration

  1. Add build configurations in Clion project in CMakeLists.txt

  • The first parameter firstlib, indicate the name of the dynamic library generated after compilation
  • The second argument can choose STATIC or SHARED, respectively, is a static library or a dynamic library, we generally use a dynamic library
  • The third and subsequent parameters, expressed the need to compile a file storage

6. Select Build-BuildProject in Clion may be generated at cmake-build-debug libfirstdylib.dylib (mac as dylib, windows as DLL)
7. The step 8 will be copied to the generated libfirstdylib.dylib libs directory under eclipse project (no new)
8. DynamicRegister.java loaded library, and the library function calls

 

Guess you like

Origin www.cnblogs.com/GX1234/p/11611410.html