JNI Java calls dynamic-link library DLLs

Source: Internet
Author: User

Now do the project to use the JNI call C language implementation of the access device DLL file, before the JNI is ignorant Kazakhstan, only heard the JNA link DLL, but just a understand.

JNI (Java Native Interface) Java Native Interface, the main purpose is to implement the Java code and C C + + written code interaction.

Theory does not say much, or look at the Java link DLL specific operation it. As follows:

The following example calls the open () method in the Connection.dll file in Java.


1,java Square

1) Loading DLL files

There are two ways of System.load (); and system.loadlibrary ();

Load requires a full path and requires a file name extension;

LoadLibrary is a specific path, under system System32 or in the project Bin directory, as long as the file name does not have a. dll extension.


2) Local Declaration method

After loading the completed DLL file, the method used is declared with the native keyword.

<span style= "FONT-SIZE:18PX;" >public class condll{     static{system.load ("D://connection.dll");//or System.loadlibrary ("Connection");     }          Public native static void Open ();} </span>

3) Generate. h header File

Use the Javah condll command to generate the. h header file (first using Javac to generate the. class file).


#include <stdio.h> #include <condll.h>jniexport void Jnicall java_condll_open (jnienv *env,jobject obj);

The method named in the generated header file is the full path and method of the Java class, separated by an underscore.

Note: If the project calls out-of-the-box DLL files, we must know the declaration of the method in C, and create the corresponding package structure in the Java program, if the path does not correspond, run the Java program will be error, personal lessons. (I did the project to adjust the package name, DLL files are not recompiled).


2,c Square

All methods are implemented according to the generated. h header file.


DLL files in a generic project are first, and in Java it is important to ensure that the package path and class name of the Java class are consistent with the method's full name path for the DLL.


JNI Java calls dynamic-link library DLLs

Related Article

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.