Eclipse中使用JNI调用C++ build的dll

本文介绍如何在 Eclipse 中通过 JNI 让 Java 应用调用 C++ 编译的 DLL 文件。文章详细说明了创建 Java 类、生成 H 文件、实现 C++ 函数、编译 DLL 和配置 Eclipse 的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Eclipse中使用JNI调用C++ build的dll
- Uper
最近想写个从java程序中调用C++生成的dll的工具.  在网上搜索一些帖子,大概过程如下:
1. 建立自己的JAVA 类,该类带有一个or多个native声明的方法
2. 使用javac,生成.CLASS 文件
3. 使用javah,生成*.H文件
4. 根据*.H文件完成C++的dll
5. copy该dll到class所在的folder下
6. 使用java运行该application 即可.
但是当我把相关内容移到eclipse上时, 总是throw exception:java.lang.UnsatisfiedLinkError异常,后发现是我的*.h文件生成有问题。
下面把我的过程描述一下,以便参考:
Eclipse side
1. Create java class named SWIFTAlianceCASmfTest with function of declared native
      package test.cs.web;
      import java.lang.*;
      
public class SWIFTAlianceCASmfTest{
            public native static int calculate(int addone);
            public static void main(String arg[]){
            try{
            SWIFTAlianceCASmfTest objTest = new SWIFTAlianceCASmfTest();
            int add1 = 3;
            int add2 = SWIFTAlianceCASmfTest.calculate(add1);
            System.out.println("The result is "+add2);
            }catch(Exception ex){
                  System.out.println("The result is "+add2);
            }  
            } 
            static {
                System.loadLibrary("mycalculate");  
            }
      }
2. Save it and eclipse will build and generate class file SWIFTAlianceCASmfTest.class in folder ../WEB-INF/CLASSES/test/cs/web/
3. Go to the folder ../WEB-INF/CLASSES/

Execute command javah -jni test.cs.web.SWIFTAlianceCASmfTest

 Note: test.cs.web is package name and not lose it.
 The file test_cs_web_SWIFTAlianceCASmfTest.h will be generated. Refer to below:

/* DO NOT EDIT THIS FILE - it is machine generated */

#include <jni.h>

/* Header for class test_cs_web_SWIFTAlianceCASmfTest */

 

#ifndef _Included_test_cs_web_SWIFTAlianceCASmfTest

#define _Included_test_cs_web_SWIFTAlianceCASmfTest

#ifdef __cplusplus

extern "C" {

#endif

/*

 * Class:     test_cs_web_SWIFTAlianceCASmfTest

 * Method:    calculate

 * Signature: (I)I

 */

JNIEXPORT jint JNICALL Java_test_cs_web_SWIFTAlianceCASmfTest_calculate

  (JNIEnv *, jclass, jint);

 

#ifdef __cplusplus

}

#endif

#endif

 

4. C++ side: According to head file test_cs_web_SWIFTAlianceCASmfTest.h, build dll named mycalculate.dll using VC and implement function Java_test_cs_web_SWIFTAlianceCASmfTest_calculate(,,)
.cpp
#include "jni.h";
#include "test_cs_web_SWIFTAlianceCASmfTest.h";
 
JNIEXPORT jint JNICALL Java_test_cs_web_SWIFTAlianceCASmfTest_calculate
(JNIEnv *env, jclass obj, jint nAdd1)
{
 return (nAdd1+nAdd1);
}
 
.def
LIBRARY LIB
EXPORTS
Java_test_cs_web_SWIFTAlianceCASmfTest_calculate @ 1
 
In VC tools, include the path of files jni.h & jni_md.h in JDK install directory.
 
5. Eclipse side: copy mycalculate.dll to folder ../WEB-INF/CLASSES/
Set Eclipse VM arguments: -Djava.library.path=<the position of mycalculate.dll located in> fgdsf
6. Run Java application in Eclipse and it will return result:The result is 6
 
以上有2点需要注意:
1. don't forget the package name when you generate head file by tool javah
2. the path of dll file and path of running command javah make is correctly.
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值