UnsatisfiedLinkError raises the JNI generated by SWIG?

I am trying to create a dynamic C library that can be derived from Java. I have compiled the 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 is the contents of the SWIG utilities.i interface file :

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

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

      

I have verified that the methods are correctly exported from the DLL and put utilities.dll in both:

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

I am using System.load(libraryPath)

to load from path 1. above, with the library name included in the path, and catch any SecurityException

or UnsatisfiedLinkError

in this call.

The library is loaded without any exception, but the library call fails:

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)

      

+2


source to share


1 answer


I found this section of the SWIG documentation that says:

The packageName and moduleName names must of course believe, otherwise you will get linker errors when the dynamic JVM loads the JNI function.

After browsing, utilities_wrap.c

I saw that my generated JNI method definitions did not contain the package name. To fix this, I added SWIG - a package command line parameter to the first line of my makefile:



swig.exe -java -package invokeoncomport utilities.i

      

My JNI method definitions now look like this and my binding error has been fixed!

SWIGEXPORT jint JNICALL Java_invokeoncomport_utilitiesJNI_set_13711a_on(...)

      

+3


source







All Articles