user947604
user947604

Reputation: 91

SWIG c++ Java DLL Windows

my problem: creating a java wrapper for a c++ DLL. I know, there are many articles about this issue but so far no solution for me. Then problem: I have java 1.6 up 29 32-bit installed on my windows 7, 64 bit, at C:\java\jdk. This path is part of path variable (open cmdline anywhere, i can always calls javac...)

I have latest swig, which successfully created python and perl wrappers for my DLL.

When i buld java/class sample with VS2008 - which completes without errors - and try to run the runme.java i get the error:

UnsatisfiedLinkError

-> Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help

the DLL is very simple. Statically linked to runtime libs (/MT). No dependecies but still getting this error.

Please note: the swig sample does not work. Java installed. Also the tcl sample does not work. tcl 8.4 installed (similar error).

Any help is appreciated. Thank you

Upvotes: 2

Views: 2395

Answers (2)

From the looks of what you've described it seems like you've not loaded the DLL you compiled within Java before trying to call one of the (generated by SWIG) JNI methods.

I normally use something like:

%pragma(java) jniclasscode=%{
  static {
    try {
        System.loadLibrary("module");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load. \n" + e);
      System.exit(1);
    }
  }
%}

in my SWIG interface file to cause the DLL to be loaded automatically at runtime by the Java code.

(You'll need to make sure the DLL is placed somewhere appropriate with respect to library paths on your system).

Upvotes: 0

Borealid
Borealid

Reputation: 98479

Your class has been compiled successfully, and the native library has been compiled successfully. The issue is that the Java code needs to load, at runtime, the shared object which you generated with Visual Studio.

Try passing -Djava.library.path=<directory containing your DLL> when you run the class.

Upvotes: 1

Related Questions