JNI Java invoke dynamic link library DLL

Source: Internet
Author: User

In the project now, we use the DLL file of the access device that JNI calls the C language implementation, before this is a Greek word for jni, only heard of the JNA link DLL, but only an understanding.

JNI (Java Native Interface) Java local interface, the main role is to implement Java code and C C + + code to interact with.

Theory is not much to say, or look at the Java link DLL specific operation bar. As follows:

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


1,java Side

1) Loading DLL file

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

The load requires a full path and requires a file name extension;

LoadLibrary is a specific path, either under the system system32 or under the Project Bin directory, as long as the file name does not have a. dll extension.


2) Local Declaration method

When the DLL file is loaded, the method that will be 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 (use Javac to generate the. class file first).

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

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

Note: If you call the existing DLL file in the project, 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 the error, hands-on lessons. (I did a project to adjust the package name, DLL file did not recompile).


2,c Side

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


In a generic project, DLL files are available first, and in Java you must ensure that the package path and class name of the Java class are the same as the method full path for the DLL.


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.