JAVA JNI Principle
JNI is an important function in the JAVA standard platform. It makes up for the shortcomings of JAVA's important advantage of platform-independent. While JAVA is cross-platform, it can also interact with dynamic libraries of other languages (such as C and C++), giving other languages the opportunity to play their advantages.
With the support of the JAVA standard platform, the JNI mode is easier to implement and use. Here is a summary of the following knowledge graph:
Example:
Environment description: ubuntu 10.4.2 LTS system
Program Listing 1: src/com/magc/jni/HelloWorld.java
/** * */ package com.magc.jni; /** * @author magc * */ public class HelloWorld { static { System.loadLibrary("Hello"); } public native void DisplayHello(); /** * @param args */ public static void main(String[] args) { new HelloWorld().DisplayHello(); } } Enter the src directory and compile the JAVA class.
Command: javac ./com/magc/jni/HelloWorld.java
Generate HelloWorld.class in the directory where HelloWorld.java is located
Then use javah to generate the header file.
Command: javah -jni com.magc.jni.HelloWorld
Generate the com_magc_jni_HelloWorld.h header file in the current directory. This file is used by C and C++ programs to reference and implement functions in it.
Program Listing 2: com_magc_jni_HelloWorld.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_magc_jni_HelloWorld */ #ifndef _Included_com_magc_jni_HelloWorld #define _Included_com_magc_jni_HelloWorld #ifdef __cplusplus extern "C" { #endif /* * Class: com_magc_jni_HelloWorld * Method: DisplayHello * Signature: ()V */ JNIEXPORT void JNICALL Java_com_magc_jni_HelloWorld_DisplayHello (JNIEnv *, job); #ifdef __cplusplus } #endif #endif Note: 1) This header file does not require user compilation and is directly used for reference by other C and C++ programs.
2) The Java_com_magc_jni_HelloWorld_DisplayHello(JNIEnv *, jobject) method in this header file is an interface for interacting with dynamic link libraries in the future, and the name must be consistent.
Listing 3 of the program: src/jni_helloworldImpl.cpp
#include <jni.h>#include "com_magc_jni_HelloWorld.h"#include <stdio.h>JNIEXPORT void JNICALL Java_com_magc_jni_HelloWorld_DisplayHello(JNIEnv *env, job obj){ printf("From jni_helloworldImpl.cpp :"); printf("Hello world ! /n"); return;} This C++ file implements the functions in the above header file, please note that the method function names must be consistent.
Compile and generate dynamic library libHello.so,
Command: g++ -shared -I /usr/lib/jvm/java-6-openjdk/include jni_helloworldImpl.cpp -o libHello.so
After success, the dynamic link library libHello.so file will be generated in the current directory.
With the dynamic library of specific implementations, you can run JAVA to call the native method of the JNI program class.
Command: java -Djava.library.path=. com.magc.jni.HelloWorld
The input result is: From jni_helloworldImpl.cpp :Hello world !
Thank you for reading, I hope it can help you. Thank you for your support for this site!