状态描述 我想采用在java中调用本地C代码,用动态链接库的方式(其他方式我还不知道有没有)就是先编写java类的native方法,之后用javah -jni命令生成.h文件。之后利用VC制作动态链接库dll,我现在在windows下尝试。
问题 现在的问题是我在java中能够加载dll文件,但是在执行本地方法的时候却不能执行,UnsatisfiedLinkError,文档中描述是找不到本地方法才抛出的异常,但是我是按照.h中的函数声明来定义的函数,请高手说说可能存在哪些错误,制作Dll要注意什么情况。多谢了!!!
问题 现在的问题是我在java中能够加载dll文件,但是在执行本地方法的时候却不能执行,UnsatisfiedLinkError,文档中描述是找不到本地方法才抛出的异常,但是我是按照.h中的函数声明来定义的函数,请高手说说可能存在哪些错误,制作Dll要注意什么情况。多谢了!!!
解决方案 »
- java throw与throws区别(不懂);
- super.paintComponent(g)绘制的背景上如何添加组件
- 为什么我在eclipse里面运行程序图片可以看见。在命令行或者是打成Jar包的情况下却是空白一片得?
- 关于 FileInputStream 的问题
- 求助: 一道简单的java程序。。。。。
- [请问]怎样统计网站点击率
- 请教:在Javascript中用escape对字符串编码,用unescape进行解码。在java中有没有等同於escape的方法?
- VVV_lucky(*太阳*) 与 wangxmwxm(网络游侠) 进来领分~~!!!
- static这个关键字该如何理解呢?
- 请koala进来取分
- 一个赋有挑战性的JAVA问题哈!(网友们帮帮忙啊!谢了哈)
- 在线急等简单问题
http://www.pconline.com.cn/pcedu/empolder/gj/java/0506/642328_2.html
#include <jni.h>
/* Header for class TestNative */#ifndef _Included_TestNative
#define _Included_TestNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestNative
* Method: set
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_TestNative_set
(JNIEnv *, jobject);#ifdef __cplusplus
}
#endif
#endif实现文件
// Dll.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
#include "Dll.h"#include "TestNative.h"BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
DLL_API int nDll=0;// This is an example of an exported function.
DLL_API int fnDll(void)
{
return 42;
}// This is the constructor of a class that has been exported.
// see Dll.h for the class definition
CDll::CDll()
{
return;
}
extern "C" JNIEXPORT void JNICALL Java_TestNative_set
(JNIEnv *, jobject){
//do some thing
}
java部分package dlmse.training;public class TestNative {
static {
System.loadLibrary("DllTest");
} public static void main(String args[]) {
System.out.println("a successful invoke!");
TestNative t = new TestNative();
t.set();
} public native void set();
}
那么,你的 C 程序应该是
JNIEXPORT void JNICALL Java_dlmse_training_TestNative_set
而你现在的程序是
JNIEXPORT void JNICALL Java_TestNative_set