The general steps for JNI program development are as follows:
L compile the call class in Java
L use javah to generate header files for C/C ++ native functions
L call other functions required in C/C ++ to implement native functions (in principle, any resource can be called)
L add all native libraries and resources dependent on the project to Java. Library. Path of the Java Project
L generate a Java program
L release Java applications and DLL Libraries
Example of JNI program development:
1. Create a New testhello. Java in the Eclipse project and enter the following content:
Public classTesthello {
Static{
System.Loadlibrary("Testhello ");
}
Public static native voidHello (string MSG );
Public static voidMain (string [] ARGs ){
Hello("Hello, kimm! ");
}
}
Compile and generate the testhello. Class file.
2. Use the javah testhello command in the command line to generate the header file testhello. H (that is, the interface of JNI proxy stub ).
3. Create a new project testhello in vc6. The project type is Win32 dynamic-Link Library. Click OK.
In the displayed window, select a simple DLL project and click Finish.
Open the stdafx. h file and add the following content at the end:
# Include <JNI. h>
# Include "testhello. H"
Open the testhello. cpp file and add a code at the end: implement the Java interface in testhello. h.
Jniexport void jnicall java_testhello_hello (jnienv * ENV, jclass OBJ, jstring jmsg)
{
Const char * strmsgptr = env-> getstringutfchars (jmsg, 0 );
MessageBox (0, strmsgptr, "message box from VC ++", 0 );
Env-> releasestringutfchars (jmsg, strmsgptr );
}
Click build-build all in the menu item on VC to generate testhello. dll.
4. Copy testhello. dll in the debug folder of the VC project to the folder where testhello. Class is located.
5. Enter Java testhello in the command line to bring up the MessageBox dialog box. Win32 API called successfully.
Public class hownetsimilarity {static {// string STR = system. getproperty ("Java. library. path "); // system. out. println (STR); system. loadlibrary ("hownetsimilarity");} public static native double similarity (string word1, string word2); public static double wordsimilarity (string word1, string word2) {return hownetsimilarity. similarity (word1, word2);} public static void main (string [] ARGs) {system. out. println (hownetsimilarity. similarity ("table", "table "));}}
/*** Uses the reflection mechanism, JNI, and calls the C ++ version of HowNet * @ Param str1 * @ Param str2 * @ return */public static double hownetsimilarity (string str1, string str2) {Class C = NULL; try {c = Class. forname ("hownetsimilarity"); // parameter class [] ptype = new class [] {class. forname ("Java. lang. string "), class. forname ("Java. lang. string ")}; // obtain method M = C. getdeclaredmethod ("wordsimilarity", ptype); // prepare an object [] as the Paras real parameter value object [] paras = new object [] {str1, str2}; Return (double) m. invoke (C. newinstance (), paras);} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} return 0 ;}
Note: In system. loadlibrary ("hownetsimilarity"), put the DLL related to HowNet that you requested in your path and put the dictionary in the root directory of your project.
They can be used to load library files, whether JNI library files or non-JNI library files. Before calling any local method, you must first load the corresponding JNI library file with one of the two methods.
2. The system. Load parameter is the absolute path of the library file, which can be any path.
For example, you can load a JNI library file on Windows:
System. Load ("C: \ Documents and Settings \ testjni. dll ");.
3. The system. loadlibrary parameter is the library file name and does not contain the library file extension.
For example, you can load a JNI library file on Windows.
System. loadlibrary ("testjni ");
Here, testjni. dll must be in the path pointed to by the JVM variable java. Library. Path.
You can use the following method to obtain the value of this variable:
System. getproperty ("Java. Library. Path ");
Put HowNet. idx and hownet_synset.idx in the project root directory.