My environment :--------
1. Java compiler: myeclipse 5.5
2. Web server: Tomcat 5.5
3. c compilers: VC ++ 6.0
4. Operating System: WINXP
5. Java source file directory: D:/study/src
6. Directory of the class file compiled by Java: D:/study/bin
7. Tomcat Directory: D:/tomcat41
1. Write a Java file: // util. Java
package com.jni;public class Util {public native static int add(int x,int y);static{System.loadLibrary("Util");}}
Compile this file to generate the util. Class file.
2. Generate the corresponding header file: open a DOS window and enter the directory for storing the class file:
When compiling a class file, you must add the package name to javah COM. JNI. util D:/> Cd D:/study/bin/> javah COM. JNI. util
In this way, the com_jni_util.h file will be generated in the Directory D:/study/bin.
3. compile the c file: in VC ++ 6.0: file-> New-> projects-> Win32 dynamic-Link Library, project name is util, and create an empty DLL project. run the com_jni_util.h file generated by 2 and the JNI file under the C:/j2sdk1.4.2/include directory. copy jni_md.h under the directory h, C:/j2sdk1.4.2/include/Win32 to the util project directory. (C:/j2sdk1.4.2 is the installation path of my jsdk ).
Create a C source file named util. C. // util. C # include "com_jni_util.h" in the util Project"
Jniexport jint jnicall java_com_jni_util_add (jnienv * ENV, jclass JC, jint X, jint y) {return X + Y ;}
4. generate util. DLL: util in compilation 3. the util. DLL file. /*************************************** * ******************************* if the following error occurs: fatal error c1083: cannot open include file: 'jni. h': no such file or directory/JDK/include/JNI. h/JDK/include/Win32/jawt_md.h/JDK/include/Win32/jni_md.h copy to ********* in the/vc7/include directory under the installation directory of visual studio.net ********* **************************************** **********************/
5. Generate a jar package: compress the util. Class file into a jar package named myutil. jar.
6. Compile the JSP file: // test. jsp <% @ page import = "com. JNI. util" %>
<HTML>
<Body>
7. set myutil. copy the jar package to the Directory D:/tomcat41/common/lib. copy the DLL file to the D:/tomcat41/bin directory (or any directory set by the Environment Variable path. copy JSP to the D:/tomcat41/webapp/test directory.
8. start Tomcat.
9. In IE, enter: http: // localhost: 8080/test. jsp. If you can see: 5 + 7 = 12, it will be successful.