JNI is the abbreviation for Java 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)
• Java classes that write methods with native declarations
• Compile the Java class written using the Javac command
• Use Javah. JNI Java class name to generate a header file with the extension h
• Implement a Local method using C + + (or other programming-like language)
• Generate a dynamic connection library for files written in C + +
1) Write Java programs:
Here take HelloWorld as an example.
Code Listing 1:
Class HelloWorld {
public native void 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: This H file is equivalent to our interface in Java, which declares a java_helloworld_ Displayhelloworld (jnienv *, Jobject) method, and then implement this method in our native method, that is, we use the method name in the C + + program to write the same as here.
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
There are some questions about using JNI for the first time, and 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 C file is JNI converted to jstring data type, declare char* test in C file, then test = (char*) (*env)->getstringutfchars (env, Jstring, NULL); note: When Test is finished, notify the virtual machine platform that the relevant code is no longer accessible: (*ENV)->releasestringutfchars (env, jstring, test);
3. Pass a char* buffer obtained in C to Java?
This char*, if it is a generic string, can be passed back as a string. If it is a buffer containing '/0 ', it is best to pass as ByteArray because the length of the copy can be set, and if copy is to string, it may be truncated to '/0 '.
has two ways to pass the resulting data:
A byte array that is directly new in JNI, and then calls the function ( *ENV)->setbytearrayregion (env, ByteArray, 0, Len, buffer); Copy the value of buffer to ByteArray, and the function will return directly to ByteArray.
One is the return error number, the data is passed as a parameter, but the basic data type of Java is a pass-through, 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 a function's argument, and assigning a byte array in Retobj is easy to pass 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.
Transferred from: http://blog.csdn.net/lion_6/archive/2008/03/17/2190442.aspx
"Java" Native keyword