When you execute swig -java test_app.i , it creates the Java glue classes that you must include in your Java project. The main interface created is called test_app.java and looks like this:
public class test_app { public static int add(int a, int b) { return test_appJNI.add(a, b); } public static int add_pointer(SWIGTYPE_p_int a, SWIGTYPE_p_int b) { return test_appJNI.add_pointer(SWIGTYPE_p_int.getCPtr(a), SWIGTYPE_p_int.getCPtr(b)); } public static String print_hello() { return test_appJNI.print_hello(); } }
As you can see, it delegates all calls to test_appJNI , which is also automatically generated, and acts like glue on its own code (using native methods):
public class test_appJNI { public final static native int add(int jarg1, int jarg2); public final static native int add_pointer(long jarg1, long jarg2); public final static native String print_hello(); }
So you should:
- include how generated
.java files in the project - make sure you download the
.so library with System.loadLibrary("test_app_wrap"); “You already did it.” - just call
test_app.print_hello()
We also recommend that you read section 21 of SWIG and Java from the SWIG manual. A preview and overview of the main sections of the C / C ++ wrapper fully explain all the basics.
A received error indicates that the JVM cannot load the .so library. My first question would be if it were compiled and properly linked. If you are sure that you have libtest_app_wrap.so , this may not be in the way where the JVM is looking for it (check this out - http://www.chilkatsoft.com/p/p_499.asp ). For instance. for me it was necessary to add -Djava.library.path=. to the Java command line:
java -Djava.library.path=. HelloWorld
For reference - I modified your HelloWorld.java file:
public class HelloWorld { static { System.loadLibrary("test_app_wrap"); } static public void main(String argv[]) { test_app.print_hello(); } }
Hope that helps :)
source share