Eclipse中使用JNI调用C++ build的dll
来源:互联网 发布:苹果6网络设置在哪 编辑:程序博客网 时间:2024/04/29 07:00
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");
}
}
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";
#include "test_cs_web_SWIFTAlianceCASmfTest.h";
JNIEXPORT jint JNICALL Java_test_cs_web_SWIFTAlianceCASmfTest_calculate
(JNIEnv *env, jclass obj, jint nAdd1)
{
return (nAdd1+nAdd1);
}
(JNIEnv *env, jclass obj, jint nAdd1)
{
return (nAdd1+nAdd1);
}
.def
LIBRARY LIB
EXPORTS
Java_test_cs_web_SWIFTAlianceCASmfTest_calculate @ 1
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.
- Eclipse中使用JNI调用C++ build的dll
- 使用 JNI 调用 c 的 DLL
- 使用JNI调用dll(C++)
- Jsp中调用DLL(使用JNI)
- 利用jni调用c的dll
- JAVA JNI调用C编写的dll
- JNI中dll调用dll
- Eclipse+JNI+MinGW编写C++,调用dll入门
- 浅谈JNI的使用--java调用dll
- java调用dll(JNI的使用)
- 浅谈JNI的使用--java调用dll
- 使用JNI,vs2010 生成dll, JSP中调用dll
- 【转载整理】eclipse 开发c/c++、Java使用JNI调用C程序、生成64位dll动态链接库
- Chap5:使用JNI技术实现java程序调用第三方dll(c/c++)文件的功能
- 使用JNI技术实现java程序调用第三方dll(c/c++)文件的功能
- JAVA中利用JNI与VS2012实现C/C++的DLL调用
- JNI 使用总结 (JAVA 调用C语言编写的DLL/SO/SL文件)
- Java使用JNI调用C/C++的DLL动态链接库1
- Heroes in my heart
- C#图书的倚天屠龙即将出版
- 可缓存的CMS系统设计
- fgets和fputs、fread和fwrite、fscanf和fprintf
- 从交流看企业文化
- Eclipse中使用JNI调用C++ build的dll
- 计算机安全知识大全
- [C++]接口继承与实现继承
- mysql ,将表B中的cid字段中的内容,插入到表A中的cid字段
- iiHero VC6下解决OCCI的办法
- 二叉树的遍历
- linux的常用文件操作命令
- asp.net技巧-统一输出脚本连接和弹消息
- Criteria 进阶查询