java代码:
public class TestDLL {
static{
System.loadLibrary("goodluck");
}
public native void hello();
public static void main(String[] args){
System.out.println(System.getProperty("java.library.path"));
System.out.println("start");
new TestDLL().hello();
}
}c头文件 javah生成
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class TestDLL */#ifndef _Included_TestDLL
#define _Included_TestDLL
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestDLL
* Method: hello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_TestDLL_hello
(JNIEnv *, jobject);#ifdef __cplusplus
}
#endif
#endifc源文件:
#include "goodluck.h"
#include "stdio.h"
JNIEXPORT void JNICALL Java_TestDLL_hello(JNIEnv * env, jclass jc){
printf("hello\n");
}执行时,总是报Exception in thread "main" java.lang.UnsatisfiedLinkError: hello
当然dll文件 在当前目录 system32下都放过。
public class TestDLL {
static{
System.loadLibrary("goodluck");
}
public native void hello();
public static void main(String[] args){
System.out.println(System.getProperty("java.library.path"));
System.out.println("start");
new TestDLL().hello();
}
}c头文件 javah生成
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class TestDLL */#ifndef _Included_TestDLL
#define _Included_TestDLL
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestDLL
* Method: hello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_TestDLL_hello
(JNIEnv *, jobject);#ifdef __cplusplus
}
#endif
#endifc源文件:
#include "goodluck.h"
#include "stdio.h"
JNIEXPORT void JNICALL Java_TestDLL_hello(JNIEnv * env, jclass jc){
printf("hello\n");
}执行时,总是报Exception in thread "main" java.lang.UnsatisfiedLinkError: hello
当然dll文件 在当前目录 system32下都放过。
无法找到共享库文件。当用 System.loadLibrary(String libname) 方法(参数是文件名)装入库文件时,请确保文件名拼写正确以及没有指定扩展名。还有,确保库文件的位置在类路径中,从而确保 JVM 可以访问该库文件。
无法找到具有指定说明的方法。确保您的 C/C++ 函数实现拥有与头文件中的函数说明相同的说明。
(摘自IBM中文网)
解决办法:在生成.h头文件前先把java程序编译一下(虽然eclipse是自动编译,手动再debug一下)然后再生成头文件->生成动态链接库->运行java程序
请高手指教、、、、、
我也遇到这样的问题,
不过我用DLLVIEW 看了,有指定的方法,
有VC可以正常使用,
为啥JAVA JNI 不成呢。