我在 CS 入门类(class)的最终项目中使用 JNI。我几乎能够让它工作,但是有一个错误困扰了我整个上周末。我查了很多地方,但没能解决。
我收到的错误:java.lang.UnsatisfiedLinkError: JNI_Functions.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))
最佳答案
如果您的类或方法名称包含下划线,则在 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 函数应称为 Java_JNI_1Functions_dllHello
。
关于java - JNI UnsatisfiedLinkError : (Class).(方法)()V,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33881435/