Java Native Interface Usage in Java
JNI is the abbreviation of Java Native Interface. Since Java 1.1, the Java Native Interface (JNI) standard has become part of the Java platform. It allows Java code to interact with code written in other languages. JNI was initially designed for locally compiled languages, especially C and C ++, but it does not prevent you from using other languages, as long as the call conventions are supported.
JNI is the abbreviation of Java Native Interface. Since Java 1.1, the Java Native Interface (JNI) standard has become part of the Java platform. It allows Java code to interact with code written in other languages. JNI was initially designed for locally compiled languages, especially C and C ++, but it does not prevent you from using other languages, as long as the call conventions are supported.
Using Java to interact with locally compiled code usually results in loss of platform portability. However, in some cases, this is acceptable or even necessary. For example, you can use old libraries to interact with hardware and operating systems, or to improve program performance. The JNI standard should at least ensure that local code can work under any Java Virtual Machine implementation.
Java class for writing methods with native declarations
· Use the javac command to compile the compiled Java class
· Use javah-JNI Java class name to generate a header file with the extension H
· Use C/C ++ to implement local methods
· Generate a dynamic connection library for files written in C/C ++
· OK
1) Write a Java program: helloworld is used as an example.
Code 1:
Class helloworld {
Public native void displayhelloworld ();
Static {
System. loadlibrary ("hello ");
}
Public static void main (string [] ARGs ){
New helloworld (). displayhelloworld ();
}
}
Declare native method: If you want to use a method as a local method, you must declare the method to native and cannot implement it. The parameters and return values of the method are described later. Load dynamic library: system. loadlibrary ("hello"); load the dynamic library (we can understand as follows: Our method displayhelloworld () is not implemented, but we use it directly below, therefore, it must be initialized before use.) Static blocks are usually used for loading. Note that the parameter "hello" of system. loadlibrary (); is the name of the dynamic library.
2) Compile
There is nothing to say about javac helloworld. Java
3) generate a header file with the extension of H
Javah-JNI helloworld
Header file content:
/* Do not edit this file-it is machine generated */
1. Include
/* Header for class helloworld */
1. ifndef _ included_helloworld 2. Define _ included_helloworld 3. ifdef _ cplusplus
Extern "C "{
1. endif
/*
* Class: helloworld
* Method: displayhelloworld
* Signature: () V
*/
Jniexport void jnicall java_helloworld_displayhelloworld (jnienv *, jobject );
1. ifdef _ cplusplus
}
1. endif 2. endif (here we can understand it as follows: This H file is equivalent to our interface in Java. Here we declare a java_helloworld_displayhelloworld (jnienv *, jobject); method, then implement this method in our local method, that is to say, the method name we used when writing the C/C ++ program must be consistent with the method name here ).
4) Compile a local method to implement the same method as the method name declared in the header file generated by the javah command.
Code 2:
1 # include "JNI. H" 2 # include "helloworld. H"
3 // # include other Headers
4 jniexport void jnicall java_helloworld_displayhelloworld (jnienv * ENV, jobject OBJ)
{
Printf ("Hello world! /N ");
Return;
}
Pay attention to the 1st lines in Code 2. H (this file can be found under the % java_home %/include folder) file introduction, because the types of jnienv and jobject in the program are defined in this header file; in addition, the helloworld. h header file introduction (I understand this as follows: It is equivalent that we can declare an interface when writing a Java program. Here we will introduce helloworld. h. The methods declared in the header file are implemented. Of course not necessarily ). Save it as helloworldimp. C and you will be OK.
5) generate a dynamic library
Here, we use Windows as an example to generate a DLL file. Use the VC compiler Cl to save the helloworldimpl. c folder. CL (lowercase letter of L)-I (uppercase letter of I, with no space between it and the following path) % java_home %/include-I % java_home %/include/Win32-LD helloworldimp. c-fehello. DLL Note: The generated DLL file name is configured after option-Fe. Here is hello, because it is in helloworld. in the Java file, the loadlibary name is hello. Of course, after modification, it also needs to be modified. In addition, you need to add the-I % java_home %/include/Win32 parameter, because the JNI. H file is introduced when compiling the local method in step 4.
6) run Java helloworld .,