Problem description:
When calling DLL using JNI, you must first generate the DLL file
Solution:
(1.exe now use the cl.exe program of vs2008to generate the DLL file
(1.1)cl.exe environment setup
Note:
The location of the cl.exe program is in the VC \ bin directory of the VS installation directory.
When you directly run the preceding program, the program automatically exits. The correct method is to add Include, path, and lib paths to the system environment variables.
Note:
The preceding configurations are required for the cl.exe program in vs2008to execute the cl command in DOS, including LIB, Path, and INCLUDE.
Corresponding to the VS2008 lib, bin, and include directories respectively
Note:
After the preceding configuration, you can run the cl command normally.
(1.2) generate the DLL file
1)
Note:
When using the cl command to generate the DLL file, we mainly call the service for JNI. Here we take the header file com_jni_HelloWorld.h generated by Javah as an example.
The cpp file containing the header file is as follows:
Note:
Because the cl command needs to contain the jni. h header file, we copy the jni. h and jni_md.h files in jdk to the VC2008 \ VC \ include directory.
Note:
Copy jni. h and jni_md.h to the include directory of VC.
2) execute commands in DOS
The following error occurs:
Fatal error LNK1104: Unable to open the file "kernel32.lib
Solution:
Note:
Copy the Kernel32.Lib file in C: \ Program Files \ Microsoft SDKs \ Windows \ v6.0A \ Lib
Under the lib directory of the VS2008 installation directory
3) generate the dll file
Note:
Under the bin \ directory of the project, run the command cl-I % java_home % \ include \ win32-LD com_jni_HelloWorld.cpp-Fehello. dll.
Dll files can be generated.
Note:
The command for generating DLL files is as follows:
By default, the VS development environment does not recognize jni. h header file. in JDK, find jni. h, and add jni. h directory to the current working path: Right-click the project, property-general property-C/C ++-add % JAVA_HOME %/include and % JAVA_HOME %/include/win32 to the additional include directory on the right.
-I indicates the link in the command-Fehello. dll is followed by the generated dll name hello. dll-LD, which is an option to create a dynamic link library.
Note:
Create DLL files in the form of multiple files
Cl-I % java_home % \ include \ win32-LD com_jni_HelloWorld.cpp
XXXX. cpp... (cpp file)-Fehello. dll
4) run the java Project
Place the generated hello. dll file under the C: \ Windows \ System32 directory
Note:
C: \ Windows \ System32 is a path that can be accessed by java.
Note:
The DLL file is successfully accessed.