The last time I learned a little bit of JNI, I did not willingly find the official documentation for JNI. (Official document is definitely a best learning material), Baidu find out some of the information is relatively fragmented, do not have the possibility of systematic learning, for beginners like me, the first comprehensive understanding of a technology than a dead corner of the study is much better. and Baidu out of part of the information is estimated to be with me such dabbler level is not yet some of the experience of the people. Therefore, personal advice is to look at official documents to fully understand a technology, and then do not understand where to go to collect information. Add your own understanding and practice, this will improve a little faster.
Well, gossip less. Into today's real question. The re-exploration of JNI
(1) When to use JNI
(2) HelloWorld of JNI
(3) Further discussion on the parameters of JNI
The beginning of this is to say a little bit about the previous one. Because of the lack of a Java foundation, this two-day reading of a part of the Java Foundation data. A knowledge point is also mentioned in the JNI official documentation: TheJava platform consists of Java VMS and Java APIs . This platform was developed to make Java applications Portable. So when a Java platform is deployed to a particular host platform, such as the Java platform of the Windows edition that we often use. It is normal for Java applications to deal with Windows itself. So the great Jni was born.
This is a picture of the official document. It can be said very directly when the understanding of the various layers of the relationship. It is also important to note that Java is not simply an interface. Should be understood as a set of written rules of use. The rules are still under study, but at the outset, I'm not in the details of how to buckle. First look at the whole. I believe that after a certain function in the work will be done. By now, JNI has been fully functional.
Having said the benefits of these jni, is it necessary to use JNI as long as there is information interaction with the host platform? The answer is certainly not.
1. If Java code is used with a weakly typed language, then Java code security is reduced
2. All the methods and properties in the Java class are visible to JNI, so the encapsulation of Java is lost.
3, JNI makes the code in the same process, the stability of the program has hidden danger
So unless you have to interact with local code in a process, try not to use JNI. There is a general principle that native methods try to define as few classes as possible.
According to the common sense of the card, here write a HelloWorld jni call.
Java code
Class HelloWorld {private native void print ();//Declaration native method, public static void main (string[] args) {new HelloWorld (). Print ();//Call this print method}static {system.loadlibrary ("HelloWorld");//system calls, static methods that run and run only once. To load a dynamic library named "HelloWorld"}
" declaring a local method in Java code must have a" native "identifier, native a decorated method that exists only as a declaration in Java code. Before calling the local method, you must first load the local library that contains the method. As shown in Helloworld.java, in a static block, when the Java VM Initializes a class, this code is executed first, which guarantees that the local library is loaded before the local method is called. " the quote is very clear about the implementation of this Java class. This is how a JNI works.
Mr. Java into Helloworld.class---------------javac Helloworld.java
Helloworld.class generating HelloWorld.h-------------javah-jni HelloWorld
The. h file is also posted here.
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class HelloWorld */#ifndef _INCLU Ded_helloworld#define _included_helloworld#ifdef __cplusplusextern "C" {#endif/* * Class: HelloWorld * Method: Print * Signature: () V */jniexport void Jnicall java_helloworld_print (JNIENV *, jobject); #ifdef __cplusplus} #endif #endif
HelloWorld.h write the corresponding. c file
C code (which needs to be compiled into a DLL, add the compiled DLL at the end of the article). )
#include <jni.h> #include <stdio.h> #include "HelloWorld.h" Jniexport void Jnicalljava_helloworld_print ( JNIEnv *env, Jobject obj) { printf ("Hello world!\n"); return;}
The next thing to do is to compile the following C code into a DLL library , copy the generated DLL to the Java directory, and print out the helloworld! when it runs. This is also the most basic implementation of a JNI.
Careful friend may find that this generated function declaration, two parameters are not used, how to analyze?
One more example.
Java code
Array.java
Class Array {private static native int[][] Initarray (int size);p ublic static void Main (string[] args) {int [] [] I2arr = in Itarray (3); for (int i = 0; i<3;i++) {for (int j = 0; j<3;j++) {System.out.print ("" + I2arr[i][j]);} System.out.println ();}} static {system.loadlibrary ("Objectarraytest");}}
C code
#include <jni.h>
#include <stdio.h>
#include "Array.h"
{sum + = Buf[i]; } return sum;}
Parameter interpretation: JNIEnv *env, Jobject obj
As for the third parameter in the second example, we will not consider it first. This is a data type of JNI, and the second example is to initialize a two-dimensional array, in which the function of the getintarrayregion is not concerned first. Take a look at the second example (*ENV), through which he can take out getintarrayregion, then the code can also see the automatic prompt is to take out other things. Then you know that this must be a structure. There is one such diagram in the document
so the first parameter is a soul of Jni , it has a function table of interface function , that is to use the standard specification of JNI, must have this parameter, then also must have jni.h this header file. This file is certainly in C:\Program files\java\jdkxxxxx\ inside, the second parameter jobject type, the code still does not reflect the specific use, (the code is written too little, comrades still need to work). The second object is equivalent to the object itself, which is the only one in C + +. But if the local method is declared as a static method, the second parameter is the object itself.
add:dll Everyone can be compiled with the command line, anyway I am too troublesome, directly with the VS2013. Create a new project--add a. c file--write code--project Properties change the configuration type in the configuration property to a dynamic library DLL, then change the Include directory, include the JDK directory, and the resulting. h file is also included, or copied to the current directory of the VS project. It is important to note that if you are a 64-bit Java environment, then compile the DLL and remember to change it to X64 in Configuration Manager and build the DLL. Otherwise, the 32-bit is generated by default. That's not going to be loaded.
Recently also in the pondering Android. Specific examples of the second parameter of JNI and some interface functions and data types are described in the next chapter. Mutual encouragement, good night!
"Re-explore JNI."