1. Introduction to JNI
JNI is the English abbreviation for Java Native interface, meaning Java local interface.
The source of the problem: because Java writing low-level application is more difficult to implement, in some of the real-time requirements of the very high part of Java is more difficult to do (real-time requirements of the place is not yet covered, real-time such topics to be studied).
Workaround: Java uses JNI to make it possible to invoke existing local libraries (c/D + + to develop any and system-related programs and class libraries) and to greatly flexibly develop Java.
2. JNI Quick Learning Tutorial
2.1 Questions:
Use JNI to write a piece of code, implement String_java_test_helloworld (JNIEnv *env, Jclass cls, jstring j_str) function, implement the string J_str ("world") before adding Hello, and returns.
2.2 Problem Solving Process:
I. Writing the Test.java class:
1 Public classtest{2 //Native Interface3 Public nativestring HelloWorld (string text);4 5 Public Static voidMain (string[] args) {6 //Load Dynamic Library7System.loadlibrary ("Test2");8Test ts =NewTest ();9String Text = Ts.helloworld ("World");Ten System.out.println (text); One } A}
Note:
1, Load dynamic Class Library: System.loadlibrary ("Test2"); "Windows is loaded under Test2.dll,linux is Test2.so"
II. Compiling the Test.java file
Enter cmd input command > Javac Test.java
Iii. Generating a Test.h file
Enter cmd input command > Javah Test
The contents of the Test.h file are as follows:
1 /*Do not EDIT this file-it are machine generated*/2#include <jni.h>3 /*Header for Class Test*/4 5 #ifndef _included_test6 #define_included_test7 #ifdef __cplusplus8 extern "C" {9 #endifTen /* One * Class:test A * Method:helloworld - * Signature: (ljava/lang/string;) ljava/lang/string; - */ the jniexport jstring jnicall java_test_helloworld -(JNIENV *, Jobject, jstring); - - #ifdef __cplusplus + } - #endif + #endif
Note:
1, function declaration, fixed format: jniexport; return type: Jstring;jni Call: Jnicall;java_ Full class Name _ Method name: Java_test_helloworld;
2, function parameter: Call jni.h encapsulated function pointer: Jnienv;java class itself: Jobject,java file incoming parameter: jstring.
Iv. writing a C language file test2.c implements the ability of the test class to invoke the dynamic-link library:
1#include"Test.h"2#include <string.h>3 4 jniexport jstring jnicall java_test_helloworld5(jnienv *env, Jobject obj, jstringstring){6 Const Char* str = (*env)->getstringutfchars (env,string,0);7 Charcap[ -];8cap[0] ='h';9cap[1] ='e';Tencap[2] ='L'; Onecap[3] ='L'; Acap[4] ='o'; - - strcat (CAP,STR); the -(*env)->releasestringutfchars (env,string,0); - return(*env)Newstringutf (env,cap); -}
Note:
1. Since Java itself uses double-byte characters, the C language itself is a single-byte character, so you need to use (*env)->getstringutfchars () to convert between Java and C string;
2, Getstringutfchars () and Newstringutf (), the first is a conversion from UTF8 to C encoding format, the second is a string based on C to return a UTF8 string;
3, Releasestringutfchars () is used to dispose of objects, there are virtual machines in Java for garbage collection, but in the C language these objects must be collected manually, otherwise it may cause a memory leak.
V. Compiling and running
Compile:
Enter cmd input command > gcc-i "D:\Program files\java\jdk1.8.0_45\include"-i "D:\Program files\java\jdk1.8.0_45\include\win32" --share Test2.c-o Test2.dll
Run:
Enter cmd input command > Java Test
The results of the operation are as follows:
HelloWorld
3. Summary:
First step: Write the Java Class (Test.java) with the native method and compile the Java class using the Javac tool (generate Test.class);
Step two: Use Javah to generate the header file (Test.h) corresponding to the native method;
The third step is to implement the corresponding header file (test2.c) using C/C + + and compile it as a dynamic-link library (test2.so).
This article runs the environment: Windows 64-bit operating system, JDK 1.8 version, MINGW64 (GCC).
4. Reference:
Http://www.cnblogs.com/icejoywoo/archive/2012/02/22/2363709.html
"Reproduced in this article please note the original address source: http://www.cnblogs.com/eesijian/p/5843879.html"
"The JNI quick Learning tutorial for Java"