【问题标题】:UnsatisfiedLinkError calling JNI generated by SWIG?UnsatisfiedLinkError 调用由 SWIG 生成的 JNI?
【发布时间】:2011-07-07 16:01:05
【问题描述】:

我正在尝试创建一个可从 Java 调用的 C 动态库。我在Cygwin 下编译了一个DLL,使用SWIG 生成具有以下makefile 的JNI:

CC= gcc -mno-cygwin
SWIG= /cygdrive/c/Documents\ and\ Settings/student/Desktop/swigwin-2.0.4/swig.exe -java 
INCLUDE1= -I/cygdrive/c/Program\ Files/Java/jdk1.6.0_25/include 
INCLUDE2= -I/cygdrive/c/Program\ Files/Java/jdk1.6.0_25/include/win32

utilities:
    ${SWIG} utilities.i
    ${CC} -c utilities.c utilities_wrap.c ${INCLUDE1} ${INCLUDE2}
    ${CC} -shared utilities.o utilities_wrap.o -Wl,--add-stdcall-alias -o utilities.dll

以下是 SWIG 接口文件 utilities.i 的内容:

/* utilities.i */
%module utilities
%{
#include "driver.h"
%}

extern int get_3711a_fd(char * device);
/* Other prototypes omitted for brevity */

我已验证方法已从 DLL 中正确导出,并将 utilities.dll 放在两者中:

  1. C:\Program Files\Java\jdk1.6.0_25\bin
  2. C:\Program Files\Java\jdk1.6.0_25\jre\bin

我使用System.load(libraryPath) 从上述路径1. 加载,路径中包含库文件名,并在该调用中捕获任何SecurityExceptionUnsatisfiedLinkError

库加载没有任何异常,但调用库失败并出现以下情况:

Exception in thread "main" java.lang.UnsatisfiedLinkError: 
invokeoncomport.utilitiesJNI.get_3711a_fd(Ljava/lang/String;)I
    at invokeoncomport.utilitiesJNI.get_3711a_fd(Native Method)
    at invokeoncomport.utilities.get_3711a_fd(utilities.java:14)
    at invokeoncomport.Main.main(Main.java:41)

【问题讨论】:

  • utilities_wrap.c 是 SWIG 生成的包装器代码吗?
  • 是的,它似乎有必要的出口;例如。 SWIGEXPORT jint JNICALL Java_utilitiesJNI_get_13711a_1fd(...)

标签: java c dll java-native-interface swig


【解决方案1】:

我找到了this section of the SWIG documentation,上面写着:

packageName 和 moduleName 必须是 当然是正确的,否则你会得到 JVM 动态时的链接器错误 加载 JNI 函数。

查看utilities_wrap.c 后,我看到我生成的JNI 方法定义不包含包名。为了解决这个问题,我将 SWIG -package command-line option 添加到我的 makefile 的第一行:

swig.exe -java -package invokeoncomport utilities.i

我的 JNI 方法定义现在如下所示,并且我的链接错误已修复!

SWIGEXPORT jint JNICALL Java_invokeoncomport_utilitiesJNI_set_13711a_on(...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    相关资源
    最近更新 更多