Looks like you're looking for a SWIG directorial feature . In your simplest form, you can use directors with SWIG by providing an interface file, for example:
%module(directors=1) MyModule %feature("director"); %{ #include "mydll.h" %} %include "mydll.h"
Given the header file "mydll.h":
class Observer { public: virtual void frobination() = 0; virtual ~Observer() {} }; inline void bar(Observer *o) { o->frobination(); }
Then you can run SWIG:
swig -Wall -java -c ++ mymodule.i
This will create three Java classes: MyModule , MyModuleJNI and Observer . These MyModule will contain all the free functions from your header file, presented as static member functions, since Java does not have functions such as free functions. You can safely ignore MyModuleJNI - this is the glue generated by SWIG to connect MyModule to real C ++ implementations. You will need to compile mymodule_wrap.cxx for MyModuleJNI (and therefore MyModule ) to work correctly and load the DLL using System.loadLibrary before you call any functions from them.
The Observer class corresponds directly to the Observer interface in mydll.h . You must extract from it in Java and override the frobinate function to give it its own implementation:
public class Test extends Observer { @Override public void frobination() { System.out.println("go go gadget frobinator"); } public static void main(String[] argv) { System.loadLibrary("mymodule"); Test t = new Test(); MyModule.bar(t); } }
What can I compile and run to do exactly what you hoped.
If you want, you can automate the call to System.loadLibrary by adding:
%pragma(java) jniclasscode=%{ static { try { System.loadLibrary("mymodule"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. \n" + e); System.exit(1); } } %}
into your SWIG interface file.
If your real header file is so simple, it should be just as easy to get the same results. If it is more complicated, you can instruct SWIG for a special occasion, some of its wraps in various ways.