Example of calling DLL in Java

Source: Internet
Author: User

best answer

1. compile the example document public class testnative {private native static int max (int A, int B); public static void main (string [] ARGs) {system. out. println (max (4, 5);} static {system. loadlibrary ("vcdll") ;}} you can specify the DLL file name in loadlibrary. you only need to rename the dynamic link library to this string. the source of native functions is the dynamic link library. 2. after preparing for this step, use javac testnative. java generates testnative. class. check syntax. 3. after the syntax check is passed, use javah testnative to generate testnative. h c ++ header file. 4. use VC ++ 6.0 to create a DLL file: <1> msdev-> New-> Project-> Win32 dynamic-Link Library <2> enter project name (e.g. vcdll) <3> choose "an empty DLL project"-> finish <4> Add testnative. H and JNI under JDK. add H and jni_md.h to the project. <5> Create a c ++ file, implements the native method. as follows: // testnative. CPP # include "testnative. H "jniexport jint jnicall java_testnative_max (jnienv * a, jclass B, jint X, jint y) {return x> Y? X: Y;} remarks: JNI. h Generally, JDK/include/can be copied to the include directory under the VC installation directory first. once and for all, JNI technology can be easily used in the future. b. the Java and VC interfaces must be considered for all data types. use jint jstring and so on. c Function Name and automatically generated. the consistency specified in the H file. the naming rule is: Java _ use class_method name 6. compile the VC ++ project. if no error occurs, the dynamic link library is generated. set the dynamic Connection Library vcdll. DLL to testnative. java directory. 7. run Java testnative. OK :-)

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.