JNI is J the abbreviation for Ava Native interface. Beginning with Java 1.1, the Java Native Interface (JNI) standard became part of the Java platform, allowing Java code to interact with code written in other languages. JNI was initially designed for local compiled languages, especially C and C + +, but it does not prevent you from using other languages, as long as the calling convention is supported.
Using Java to interact with locally compiled code often loses platform portability. However, in some cases this is acceptable and even necessary, such as using some old libraries, interacting with the hardware, the operating system, or to improve the performance of the program. The JNI standard guarantees at least local code to work with any Java virtual machine implementation.
Writing steps for JNI (Java Native Interface)
write Java class with native declaration method
using the Javac command to compile the Java class
using the Javah. JNI Java class name to generate a header file with the extension H
Implementing a local method using C + + (or other programming-like languages)
generate a dynamic connection library for files written in C + +
1) write Java program:
here take HelloWorld as an example.
Code 1:
class HelloWorld { publicnativevoid displayhelloworld (); Static { system.loadlibrary ("Hello"); } Public Static void Main (string[] args) { new HelloWorld (). Displayhelloworld ();} }
Declaring the native method: If you want to use a method as a local method, then you must declare that the change method is native and cannot be implemented. The parameters and return values of the methods are described later.
Load Dynamic Library: system.loadlibrary ("Hello"); Load dynamic Library (we can understand this: our method Displayhelloworld () is not implemented, but we use it directly below, So it has to be initialized before use) and it is usually loaded with a static block. It is also important to note that system.loadlibrary (), the parameter "Hello" is the name of the dynamic library.
Main () method
2) Compile there's nothing to say.
Javac Helloworld.java
3) Generate header file with extension h
Javah. JNI HelloWorld
The contents of the header file:
/* Don't EDIT this file-it are machine generated */
#include
/* Header for class HelloWorld */
#ifndef _included_helloworld
#define _included_helloworld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:helloworld
* Method:displayhelloworld
* Signature: () V
*/
jniexport void Jnicall Java_helloworld_displayhelloworld
(JNIENV *, jobject);
#ifdef __cplusplus
}
#endif
#endif
(Here we can understand: This h file is equivalent to our interface in Java, this declares a java_helloworld_displayhelloworld (jnienv *,jobject); Then we implement this method in our native method, that is, we must use the same method name as the one used to write C + + programs.
4) The method that writes the local method
implementation and the same method name declared inside the header file generated by the Javah command.
Code 2:
1 #include
2 #include "HelloWorld.h"
3 #include
4 jniexport void Jnicall Java_helloworld_displayhelloworld (jnienv *env, Jobject obj)
{
printf ("Hello world!\n");
Return
}
Note the 1th line in code 2, you need to jni.h (the file can be found under the%java_home%/include folder) file introduction, because in the program jnienv, Jobject and other types are defined in the header file In addition, the HelloWorld.h header file needs to be introduced in line 2nd (as I understand it: the equivalent of implementing an interface when writing a Java program requires a declaration to be implemented, and this is how the HelloWorld.h header file is declared. This is not necessarily the case, of course). Then save it as helloworldimpl.c OK.
5) Create a dynamic library
Here, for example in Windows, you need to generate a DLL file. Under Save Helloworldimpl.c folder, use VC's compiler to CL.
Cl-i%java_home%\include-i%java_home%\include\win32-ld Helloworldimp.c-fehello.dll
Note: The generated DLL file name is configured after the option-fe, here is hello, because in the Helloworld.java file we loadlibary used the name is hello. Of course there is a need to modify it after this change. It is also necessary to add the-i%java_home%\include-i%java_home%\include\win32 parameter because the jni.h file is introduced when writing the local method in step fourth.
6) Running the program
Java HelloWorld is OK.
issues considered in the JNI (Java Native Interface) Call
some questions when using JNI for the first time, later in one by one, the following are the memos for these issues:
1. How is Java and C interoperable?
is not the main reason for interoperability is the problem of data type, JNI solves this problem, for example, the C file of the Jstring data type is the Java incoming string object, The transformation of the JNI function can become the char* of C.
corresponding data type relationships are as follows:
Java type local C type description
boolean Jboolean unsigned, 8-bit
byte jbyte unsigned, 8-bit
Char Jchar unsigned, 16-bit
short Jshort signed, 16-bit
int Jint signed, 32-bit
long Jlong signed, 64-bit
Float jfloat 32-bit
double jdouble 64-bit
void void N/a
2. How do I convert a Java-passed string parameter to C's char* and then use the?
Java passed in the string parameter, in the C file is Jni converted to the Jstring data type, in the C file declared char* test, and then test = (char*) (*env)->getstringutfchars (env, Jstring,null); Note: When the test is finished, notify the virtual machine platform that the relevant code no longer needs to be accessed: (*env)->releasestringutfchars (env,jstring, test);
3. Pass a char* buffer obtained in C to Java?
If this char* is a generic string, it can be passed back as a string. If it is a buffer containing ' bytearray ', it is best to spread out as a copy, because it can be set to the length, if the copy to a string, may be truncated.
There are two ways to pass the resulting data:
One is to add a byte array directly to the JNI and then call the function (*env)->setbytearrayregion (env,bytearray, 0, Len, buffer); Copy the value of buffer to ByteArray , the function can be returnbytearray directly.
One is the return error number, the data is passed as a parameter, but the basic data type of Java is a value, and the object is a passing reference, so the byte array that needs to be outgoing is wrapped with a class, as follows:
Class Retobj
{
Public byte[] ByteArray;
}
This object is retobj out as the parameter of the function, and the byte array in the Retobj is assigned to be easily outgoing through the following function. The code is as follows:
Jclass CLS;
Jfieldid FID;
Jbytearray ByteArray;
ByteArray = (*env)->newbytearray (Env,len);
(*env)->setbytearrayregion (env, ByteArray, 0, Len, buffer);
CLS = (*env)->getobjectclass (env, retobj);
FID = (*env)->getfieldid (env, CLS, "Retbytes", "[B"]);
(*env)->setobjectfield (env, retobj, FID, ByteArray);
4. How much space does not know how to use buffer, how to pass out?
In the JNI C file, new out of space, passed out. Java data is not initialized, pointing to the space passed out.
Native keywords in Java