Compile a Java program:
The following uses helloworld 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.
Main () method
2) There is nothing to say about compilation.
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 */
# 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 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 local methods
Implement the same method as the method name declared in 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;
}
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 ). Then save it as helloworldimpl. C and then 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-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 the name we use when loadlibary is hello in the helloworld. Java file. 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 the program
Java helloworld is OK. Pai_^