我的代码如下
Java文件
//SystemInformation.java
package test;
public class SystemInformation {
static{
try{
System.loadLibrary("SysInfo");
}catch(UnsatisfiedLinkError e){
System.out.println ("native lib '" + "SystemInformation" + "' not found in 'java.library.path': "
+ System.getProperty ("java.library.path"));
throw e; // re-throw
}
}
public native static int getDouble(int n);
public int GetDouble(int n){
return getDouble(n);
}
}
创建DLL的C ++代码
//SysInfo.cpp
#include "jni.h"
JNIEXPORT jint JNICALL Java_test_SystemInformation_getDouble
(JNIEnv *env, jclass jcls, jint n) {
return n * 2;
}
由Javah.exe生成的标头
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class test_SystemInformation */
#ifndef _Included_test_SystemInformation
#define _Included_test_SystemInformation
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: test_SystemInformation
* Method: getDouble
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_test_SystemInformation_getDouble
(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif
#endif
当我调用loadLibrary时,我没有异常(&#34; SysInfo&#34;)
当我尝试从main调用GetDouble函数时,它会在控制台
中抛出错误消息//Main.java
package test;
public class Main {
public static void main(String[] args){
SystemInformation sInfo = new SystemInformation();
long l = sInfo.GetDouble(4);
System.out.println("Double Value = " + l);
System.exit(0);
}
}
我得到的错误是
Exception in thread "main" java.lang.UnsatisfiedLinkError: test.SystemInformation.getDouble(I)I
at test.SystemInformation.getDouble(Native Method)
at test.SystemInformation.GetDouble(SystemInformation.java:18)
at test.Main.main(Main.java:6)
我尝试了很多选项,但没有一个工作
DLL正在加载正确,因为我没有在System.loadLibrary(&#34; SysInfo&#34;)时出现异常;被称为
这是功能的签名
?Java_test_SystemInformation_getDouble @@ YAJPEAUJNIEnv _ @@ PEAV_jclass @@Ĵ@ Z
任何建议都将受到赞赏。