Teach you to use DLL methods in Java

Source: Internet
Author: User

The Java language itself is cross-platform, and if the technology for invoking DLLs through Java is easy to use, using Java to develop the foreground interface can be faster and more cross-platform.

When Java invokes a DLL library that is written by C + +, it is prone to problems in parameter passing because of different basic data types and possible differences in the use of byte sequences.

Scenarios that use Java to invoke DLL dynamic-link libraries usually have three kinds: JNI, Jawin, Jacob. where JNI (Java Native Interface) is a method of invoking a locally compiled library of functions provided by the Java language itself, it is cross-platform and can invoke different local libraries on different machines. Both Jawin and Jacob are sourceforge.net open source projects that are based on JNI technologies that rely on the implementation of Windows to make it easier to use COM and DLLs under Windows platforms.

Third, JNI

Sun Related documents: http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html

http://www.bt285.cn/or http://www.5a520.cn/.

Complete example of JNI: http://www.pconline.com.cn/pcedu/empolder/gj/java/0506/642328.html

The application scenarios for JNI are mapped based on Java classes and local functions. The steps to use DLLs are relatively cumbersome, involving not only Java programming, but also C/s programming.

The steps to use JNI are:

1. Write a Java class that uses this class to declare a function service that the DLL provides externally, where the Java method is declared as native, and its method signature can be customized without implementing the function body.

2. Use the Javah tool to generate the corresponding. h header file for this Java class.

3. The most important and troublesome step: to write the functions declared in the C/h header file, which contains the jni.h header file, and is programmed using the data type defined in the code as the input and return data type of the function. Data type conversions are implemented in this way. For example, the data type: Boolean (Java) Àjboolean (jni.h:typedef unsigned char jboolean), using the data type Jboolean in its own compiled/C code to map a Boolean type in Java. In this step, you can call a DLL library that already exists in C + + code.

4. You can use the Java class when you write another Java code.

In step 3rd, when you write the C/a function, you can use a env pointer called interface pointer to invoke a series of (many) functions provided by JNI, using these functions to access the objects and data of the JVM.

The disadvantage of using JNI is that it is cumbersome to use, and you need to encapsulate existing DLLs, and you need to know more about C + +.

Advantages of using JNI: You can invoke the local library across platforms.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.