Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the principle of JNI in Java

Shulou Source: shulou.com Published: 2022-06-02 04:05:54 09月22日 Update

This article mainly explains "how to understand the principle of JNI in Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the principle of JNI in Java".

Example:

Environment description: ubuntu 10.4.2 LTS system

Program list 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 () }}

Go to the src directory and compile the JAVA class

Command: javac. / com/magc/jni/HelloWorld.java

Generate HelloWorld.class in the directory where the HelloWorld.java is located

Then use javah to generate the header file

Command: javah-jni com.magc.jni.HelloWorld

Generate the com_magc_jni_ HelloWorldH header file under the current directory, which can be referenced by C and C++ programs and implement the functions in it.

Program list 2:com_magc_jni_HelloWorld.h

Note: 1) this header file does not need to be compiled by the user and is provided directly for it.

/ * DO NOT EDIT THIS FILE-it is machine generated * / # include / * 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 * Jobject) # ifdef _ _ cplusplus} # endif # endif

It C, C++ program reference.

2) the Java_com_magc_jni_HelloWorld_DisplayHello (JNIEnv *, jobject) method in this header file is the interface to interact with the dynamic link library in the future, and the name needs to be consistent.

Program list 3:src/jni_helloworldImpl.cpp

# include # include "com_magc_jni_HelloWorld.h" # include JNIEXPORT void JNICALL Java_com_magc_jni_HelloWorld_DisplayHello (JNIEnv * env, jobject obj) {printf ("From jni_helloworldImpl.cpp:"); printf ("Hello world!\ n"); return;}

This C++ file implements the functions in the above header file, and note that the name of the method function should be consistent.

Compile and generate dynamic library libHello.so

Command: Gmail +-shared-I / usr/lib/jvm/java-6-openjdk/include jni_helloworldImpl.cpp-o libHello.so

When successful, the dynamic link library libHello.so file is generated in the current directory.

Once you have the implemented dynamic library, 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, the above is the content of "how to understand the principle of JNI in Java". After the study of this article, I believe you have a deeper understanding of how to understand the principle of JNI in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: File program generation principle dynamic command directory function method list Clearing learning compilation consistency content link success name instance that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Linux OPPO Reno Docker