As crawler development requires a function for Image Parsing, it finds some JNI content on the network and makes some modifications, as shown below:
Currently, Java and DLL interaction technologies mainly include JNI, jawin, and Jacob. JNI (Java Native Interface) is a technology provided by Sun for Java to interact with native methods in the system (in Windows/Linux systems, Java and native method can be intertuned ). Currently, it can only be implemented by C/C ++. The last two are both open-source projects on SourceForge and application libraries on Windows systems based on JNI technology. Jacob (Java-com Bridge) provides the ability for Java programs to call methods in Microsoft's COM object. In addition to COM objects, jawin (Java/Win32 Integration Project) can also win32-dll methods in the dynamic link library. In terms of functions: JNI> jawin> Jacob, its general structure is as follows:
JNI Technical Architecture
In terms of usability, the opposite is true: Jacob> jawin> JNI.
JVM encapsulates the differences between various operating systems and provides JNI technology so that developers can use Java programs (CODE) to call the library functions implemented by operating system-related technologies, to interact with other technologies and systems, and use other technologies to implement system functions; at the same time, other technologies and systems can use the corresponding native interfaces provided by JNI to call functions implemented inside the Java application system.
In Windows, executable applications are generally based on the native PE structure, and the JVM on Windows is also implemented based on the native structure. Java application systems are built on JVM.
Java on Windows
JNI can be seen as a proxy mode for the application itself. Developers need to use C/C ++ to implement a proxy Program (JNI program) to actually operate the target native function, in Java programs, the JVM indirectly calls the target native function by loading and calling the JNI program.
JNI call Process
L the general steps for JNI program development are as follows:
1. Compile the calling class in Java and use the javac package name. *. Java command to obtain the class file of the calling class.
2. Use the javah package name. * to generate the header file of the C/C ++ native Function
3. Call other required functions in C/C ++ to implement native functions (in principle, any resource can be called)
4. Add all native libraries and resources dependent on the project to Java. Library. Path of the Java project.
5. Generate the dll library corresponding to the Java call class
6. Release Java applications and DLL Libraries
L JNI program development example:
1. Create a New testhello. Java in the Eclipse project and enter the following content:
Public class testhello {
Static {
System. loadlibrary ("testhello"); // load the dynamic link library file named testhello. dll
}
// Local method declaration
Public static native void Hello (string MSG );
Public static void main (string [] ARGs ){
// Call the local method
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 file directory of the project and copy the testhello. h file generated in step 2 to this directory. Click FileView in the middle of the Left to switch to the file browsing mode. Right-click header files and choose add files to folder ....
Select the testhello. h file and click OK.
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:
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 );
}
On the VC menu, select Tools-OPTIONS ..., Open the option dialog box. In the directories folder, add the include and include/Win32 folders under the JDK folder.
Click build-build all in the menu item on VC to generate testhello. dll. Alternatively, right-click header files and select add files to folder to find the files under the JDK directory respectively. For example, in my JDK directory, set C: /program files/Java/jdk1.6.0/include/JNI. h, C:/program files/Java/jdk1.6.0/include/Win32/jni_md.h, which is the same as importing a head file.
4. Copy testhello. dll in the debug folder of the VC project to the folder where testhello. Class is located.
If the following exception occurs: Java. Library. path is not set or the setting is incorrect.
Exception in thread "Main" Java. Lang. unsatisfiedlinkerror: No testhello in Java. Library. Path
At java. Lang. classloader. loadlibrary (classloader. Java: 1682)
At java. Lang. runtime. loadlibrary0 (runtime. Java: 823)
At java. Lang. system. loadlibrary (system. Java: 1030)
But the above exceptions do not matter, we just need to put testhello. h under the C:/Windows/system32 directory, the system will automatically find this DLL
C:/Windows/system32
5. Enter Java testhello in the command line to bring up the MessageBox dialog box. Win32 API called successfully
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shibenjie/archive/2009/06/04/4238287.aspx