Java Call DLL

Source: Internet
Author: User

How can a function in a DLL be called in Java?

The key is that the native function name parameter in Java is identical to the local function name parameter in the DLL.


This program is the SUM function in the call DLL in Java.


One, Java code part operation


1. New Project


2.dlltest.java Code

Package Com.aizizai.calldll;public class Dlltest {static {system.loadlibrary ("Hellodll");} public static native int sum (int a, int b);p ublic static void Main (string[] args) {System.out.println ("sum (2,5) =" + SUM ( 2, 5));}}

3. Generating the header file

Java Call DLL Test \src>javac Com/aizizai/calldll/dlltest.java
Java Call DLL Test \src>javah com.aizizai.calldll.DllTest


Second, VC uses the previous header file to generate the DLL. This article uses the VS2010.


1. Create a project

New Project--VC + +-Win32 Project--named Hellodll---Application type: DLL-->ok.

2. Add a header file

A. Put

/jdk/include/jni.h

/jdk/include/win32/jawt_md.h

/jdk/include/win32/jni_md.h

Copy to the project directory or to the include directory of VS.

B. Add in StdAfx.h

#include "Com_aizizai_calldll_dlltest.h"

3. Add a function implementation in the source file

Hellodll.cpp: Defines an export function for a DLL application. #include "stdafx.h" Jniexport jint jnicall java_com_aizizai_calldll_dlltest_sum (jnienv *env, Jclass SS, Jint A, Jint b) { return a+b;}

4. Building a DLL

Build---> Build DLL.

Can be seen in the debug directory of the project, Hellodll.dll file


Third, use

Copy the DLL file to the C:\Windows directory and run the Java program. Ok.



Program: http://download.csdn.net/detail/u012587637/8065041










Java Call 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.