1. Compile a Java class with native declarations
Create helloworld. Java
Class helloworld {
PublicNativeVoid displayhelloworld ();
Static {
System. loadlibrary ("helloworldimpl ");
}
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 ("helloworldimpl"); load the dynamic library (we can understand it 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 "hello" parameter of system. loadlibrary (); is the name of the dynamic library.
2. Use the javac command to compile the compiled Java class
3. Use javah-JNI Java class name to generate a header file with the extension H
Get helloworld. h
/* Do not edit this file-it is machine generated */
# Include "JNI. H"
/* Header for class helloworld */
# Ifndef _ included_helloworld
# DEFINE _ included_helloworld
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: helloworld
* Method: displayhelloworld
* Signature: () V
*/
JniexportVoid jnicall java_helloworld_displayhelloworld
(Jnienv *, jobject);
# Ifdef _ cplusplus
}
# Endif
# Endif
Here we can understand 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, we are writing C/C ++ProgramThe method name must be the same as the method name here.
4. Use C/C ++ to implement local methods
Create helloworldimpl. cpp
# Include "JNI. H"
# Include "helloworld. H"
Jniexport void jnicall java_helloworld_displayhelloworld (jnienv * ENV, jobject OBJ ){
Printf ("Hello world! \ N ");
Return;
}
5. Generate a dynamic connection library for files written in C/C ++
I use the following methods:
5.1 copy from % java_home %/include
The directory is the same as helloworldimpl. cpp.
5.2 run Cl/LD helloworldimpl. cpp to obtain the helloworldimpl. dll file.
The following methods are also used:
To generate a dynamic library, take windows as an example. You need to generate a DLL file. In the helloworldimpl. cpp folder, use the VC compiler Cl to generate. CL-I % java_home % \ include \ Win32-I "% cpp_home %" \ include-LD helloworldimpl. CPP-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-I "% cpp_home %" \ include parameter, this is because JNI is introduced when writing local methods in step 4. h file (which files are introduced must contain the parameters of these files ). Note: add the environment variable Lib = % cpp_home % \ Lib (cpp_home = E: \ Program Files \ Microsoft Visual Studio \ vc98 on my machine ), because it needs to be linked to some lib files in it
6. Run the class command to get the result. Note: all the above process files are in the same folder.