1. Introduction to JNI
JNI is the English abbreviation of Java Native Interface, meaning Java local interface.
Source of the problem: Since it is difficult to implement the underlying application of Java writing, Java is difficult to handle in some parts with very high real-time requirements (there are no areas with high real-time requirements yet, and topics like real-time need to be investigated).
Solution: Java uses JNI to call existing local libraries (C/C++ to develop any system-related programs and class libraries), which greatly flexibly develop Java.
2. JNI Quick Learning Tutorial
2.1 Question:
Write a piece of code using JNI, implement the string_Java_Test_helloworld(JNIEnv *env, jclass cls, jstring j_str) function, implement the addition of hello in front of the string j_str("world") and return.
2.2 Problem resolution process:
I. Write the Test.java class:
public class Test{ // native interface public native String helloworld(String text); public static void main(String[] args){ // Load dynamic library System.loadLibrary("Test2"); Test ts = new Test(); String text = ts.helloworld("world"); System.out.println(text); }}Remark:
1. Load dynamic class library: System.loadLibrary("Test2"); [The one that is loaded in Windows is Test2.dll, and the one that is loaded in Linux is Test2.so]
II. Compile Test.java file
Enter cmd to enter command > javac Test.java
III. Generate Test.h file
Enter cmd input command > javah Test
The contents of the Test.h file are as follows:
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class Test */#ifndef _Included_Test#define _Included_Test#ifdef __cplusplusextern "C" {#endif/* * Class: Test * Method: helloworld * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_Test_helloworld (JNIEnv *, jobject, jstring);#ifdef __cplusplus}#endif#endifRemark:
1. Function declaration, fixed format: JNIEXPORT; return type: jstring; JNI call: JNICALL; Java_full class name_method name: Java_Test_helloworld;
2. Function parameters: Call jni.h to encapsulated function pointer: JNIEnv; Java class itself: jobject, Java file passed parameters: jstring.
IV. Write C language file Test2.c to implement the function of the Test class calling dynamic link library:
#include "Test.h"#include <string.h>JNIEXPORT jstring JNICALL Java_Test_helloworld (JNIEnv *env, job obj, jstring string){ const char* str = (*env)->GetStringUTFChars(env,string,0); char cap[128]; cap[0] = 'h'; cap[1] = 'e'; cap[2] = 'l'; cap[3] = 'l'; cap[4] = 'o'; strcat(cap,str); (*env)->ReleaseStringUTFChars(env,string,0); return (*env)->NewStringUTF(env,cap); }Remark:
1. Since Java itself uses double-byte characters, C language itself is single-byte characters, so you need to use (*env)->GetStringUTFChars() to convert the string between Java and C;
2. GetStringUTFChars() and NewStringUTF(). The first is the encoding format converted from UTF8 to C, and the second is to return a UTF8 string based on the string of C;
3. ReleaseStringUTFChars() is used to release objects. There are virtual machines in Java for garbage collection, but in C language, these objects must be manually recycled, otherwise memory leaks may occur.
V. Compile and run
Compilation:
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 to enter command > java Test
The operation results are as follows:
helloworld
3. Summary:
Step 1: Write a Java class (Test.java) with native methods, and use the javac tool to compile the Java class (generate Test.class);
Step 2: Use javah to generate the header file (Test.h) corresponding to the native method;
Step 3: Use C/C++ to implement the corresponding header file (Test2.c) and compile it into a dynamic link library (Test2.so).
The running environment of this article: Windows 64-bit operating system, JDK version 1.8, mingw64 (GCC).
The above Java JNI quick introduction tutorial (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.