In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.