java JNI未满足链接错误:(类)。(方法)V
我在CS课程介绍的最后一个项目中使用了JNI。我几乎可以让它工作,但有一个错误,这是困扰我整个周末。我在很多地方都找过了,但都没能修好
我得到的错误是:java。lang.UnsatifiedLinkError:JNI_函数。dllHello()V
我的代码:
////////////////////////
// JNI_Functions.c
#include "JNI_Functions.h"
#include <jni.h>
#include <stdio.h>
JNIEXPORT void JNICALL Java_JNI_1Functions_dllHello(JNIEnv *env, jobject obj) {
printf("This was sent from the DLL\n");
return;
}
///////////////////////
// JNI_Functions.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNI_Functions */
#ifndef _Included_JNI_Functions
#define _Included_JNI_Functions
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNI_Functions
* Method: dllHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_JNI_1Functions_dllHello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
//////////////////// JAVA ///////////////////
// Entry.java
public class Entry {
public static void main(String[] args) {
try {
// JNI_Functions jni = new JNI_Functions();
// jni.dllHello();
new JNI_Functions().dllHello();
} catch (UnsatisfiedLinkError e) {
System.out.println("Couldn't call native function.\n" + e);
}
}
}
///////////////////////
// JNI_Functions.java
public class JNI_Functions {
public JNI_Functions() {
System.loadLibrary("JNI_Functions");
System.out.println("Loaded JNI_Functions.dll");
}
// Prints out a simple hello world from the dll
public native void dllHello();
}
我正在使用Code::Blocks进行编译,如果需要,下面是输出:
-------------- Build: Release in JNI_Functions (compiler: GNU GCC Compiler)---------------
mingw32-gcc.exe -Wall -O2 -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -ID:\JNITest -c D:\JNI_Functions\JNI_Functions.c -o obj\Release\JNI_Functions.o
mingw32-g++.exe -shared -Wl,--output-def=bin\Release\libJNI_Functions.def -Wl,--out-implib=bin\Release\libJNI_Functions.a -Wl,--dll obj\Release\JNI_Functions.o -o bin\Release\JNI_Functions.dll -s
Output file is bin\Release\JNI_Functions.dll with size 9.00 KB
Process terminated with status 0 (0 minute(s), 9 second(s))
0 error(s), 0 warning(s) (0 minute(s), 9 second(s))
# 1 楼答案
如果类或方法名包含下划线,则在C中它将变成
_1
,而不是_
。这是因为JNI_Functions.method
和JNI.Functions_method
没有相同的C名称见http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html中的表2-1
在您的例子中,C函数应该被调用^{