JNI introductory Tutorial HelloWorld

xiaoxiao2021-03-06  71

This article tells how to use JNI technology to implement HelloWorld, the purpose is to let readers familiar with the mechanism of JNI and write the first HelloWorld program.

Java Native Interface (JNI) is a local programming interface for the Java language, part of J2SDK. In a Java program, we can implement some functions that are inconvenient to use by Java language through JNI. There is usually in the following cases that we need to use JNI to implement.

The standard Java class library does not provide the features you need to provide, usually these functions are platform-related. You want to use some existing class libraries or applications, and they are not using some part of the program written in Java language. Speed ​​requirements are more demanding, you choose to use assembly or C language to implement and call them in Java language

In "Java Core Technology", the author mentioned JNI, it is recommended not to use JNI technology, on the one hand it needs you to master more knowledge can be controlled, on the one hand, using JNI your program will lose Portability. In this article we skip the underlying mechanism of JNI, the reader is best to imagine it as a local code and Java code adhesive. The relationship is shown below:

Below we start writing the HelloWorld program, because we should use the Microsoft VC tool in the development of the C / C code.

Write Java code We create a Hello directory on the hard disk as our work directory, first we need to write your own Java code, in the Java code we will declare the Native method, the code is very simple. FIG class HelloWorld {public native void displayHelloWorld (); static {System.loadLibrary ( "hello");} public static void main (String [] args) {new HelloWorld () displayHelloWorld ();.}} Our attention as DISPLAYHELLOWORLD () method declaration, it has a keyword native, indicating that this method is implemented using language other than Java. The method does not include implementation because we have to implement it in a C / C language. Note SYSTEM.LOADLIBRARY ("Hello") This code is defined in a static initialization block, the system is used to load the Hello shared library, which is the Hello.dll we generated later (if other operating system may be Other forms, such as Hello.so) Compile Java code Javac HelloWorld.java Generate HelloWorld.class file creation .h files In this step we are using the javah command to generate .h file, this file is going to use in the back C / C code To, we run Javah HelloWorld. This way we can see a HelloWorld.h file in the same directory, and the contents of the file are here we don't explain too much. / * DO NOT EDIT THIS FILE - it is machine generated * / # include / * Header for class HelloWorld * / # ifndef _Included_HelloWorld # define _Included_HelloWorld # ifdef __cplusplusextern "C" {# endif / * * Class: HelloWorld * Method: displayHelloWorld * Signature: () V * / JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject); #} # endif # endif write native code that implements defined in this section we use the C / C language in java ifdef __cplusplus method, we create in VC in a Project, and then create a HelloWorldImp.cpp file, as follows #include #include "HelloWorld.h" #include JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv * env, Jobject obj) {Printf ("Hello World! / N"); return;} Note We here include jni.h and the HelloWorld.h file just got. So you want to set it in VC , jni.h is in Java_Home / Include. After compilation, it will be regenerated into a Hello.dll file.

Run the Java program to copy the hello.dll files above to our work directory, then our directory includes helloworld.java, helloworld.class, and hello.dll files. Run the Java HelloWorld command, you can see the output of Hello World | at the console. JNI is a complex, interesting and challenging technology. We will explain how to interact between Java language and Native language in several articles.

转载请注明原文地址:https://www.9cbs.com/read-91885.html

New Post(0)