Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the standard principle of JNI in JAVA development?

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the standard principle of JNI in JAVA development". In daily operation, I believe that many people have doubts about the principle of JNI standard in JAVA development. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "what is the principle of JNI standard in JAVA development?" Next, please follow the editor to study!

JNI is the abbreviation of Java Native Interface, which means JAVA local call in Chinese.

JNI is an important function of the JAVA standard platform, which makes up for the deficiency of the platform-independent advantage of JAVA. While JAVA implements cross-platform, it can also interact with the dynamic libraries of other languages (such as C, C++), giving other languages the opportunity to exert their advantages.

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

/ * 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

Note: 1), this header file does not need to be compiled by the user and can be directly referenced by other C, C++ programs.

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!

At this point, the study on "what is the standard principle of JNI in JAVA development" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report