UnsatisfiedLinkError calling JNI generated by SWIG?

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I'm trying to create a C dynamic library that's callable from Java. I've compiled a DLL under Cygwin, using SWIG to generate the JNI with the following makefile:

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

Here are the contents of the SWIG interface file utilities.i:

/* utilities.i */ %module utilities %{ #include "driver.h" %}  extern int get_3711a_fd(char * device); /* Other prototypes omitted for brevity */

I've verified that the methods are being correctly exported from the DLL, and placed utilities.dll in both:

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

I use System.load(libraryPath) to load from path 1. above, with the library file-name included in the path, and catch any SecurityException or UnsatisfiedLinkError on that call.

The library loads without any exceptions, but calling the library fails with the following:

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)

回答1:

I found this section of the SWIG documentation, which says:

The packageName and moduleName must of course be correct else you will get linker errors when the JVM dynamically loads the JNI function.

After looking at utilities_wrap.c, I saw my generated JNI method definitions did not contain a package name. To fix this, I added the SWIG -package command-line option to the first line of my makefile:

swig.exe -java -package invokeoncomport utilities.i

My JNI methods definitions now look like the following, and my linking error is fixed!

SWIGEXPORT jint JNICALL Java_invokeoncomport_utilitiesJNI_set_13711a_on(...)


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!