From: Dan N. <Dan.Nietupski@GlobalGeophysical.com> - 2014-01-10 22:04:59
|
Hi, I have a very simple question and example about the use of longs when interfacing with Java. SWIG is insisting to change longs to ints. Why and can I get around it? Here is example.i: %module example %{ void tryLong(long); %} void tryLong(long); Here is example.cxx: #include <stdio.h> void tryLong(long myLong) { printf("myLong=%ld\n",myLong); } Here is a fragment of the automatically generated C++ wrapper code. Notice that SWIG changed the argument to tryLong to jint and then used an intermediate cast to long before calling tryLong with a long. JNIEXPORT void JNICALL Java_exampleJNI_tryLong(JNIEnv *jenv, jclass jcls, jint jarg1) { long arg1 ; (void)jenv; (void)jcls; arg1 = (long)jarg1; tryLong(arg1); Here is the automatically generated example.java: public class example { public static void tryLong(int arg0) { exampleJNI.tryLong(arg0); } } This is causing me trouble because my main code is Java that has longs that I need to send to native code. Additional Info. Using swig 2.0.11 on Linux (CentOS release 5.6 (Final)). This is how I ran the example: setenv CLASSPATH . setenv LD_LIBRARY_PATH . swig -java -c++ example.i g++ -c example.cxx example_wrap.cxx -fpic -fno-strict-aliasing -I$JDK_HOME/include -I$JDK_HOME/include/linux g++ -shared example.o example_wrap.o -o libexample.so javac *.java java tester Here is tester.java where I was forced to use an int: public class tester { public static void main(String argv[]) { System.loadLibrary("example"); System.out.println("example"); int myLong = 1; example.tryLong(myLong); } } If I use a long, I get this error: tester.java:8: tryLong(int) in example cannot be applied to (long) example.tryLong(myLong); ^ 1 error Exception in thread "main" java.lang.NoClassDefFoundError: tester Caused by: java.lang.ClassNotFoundException: tester at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) Here is the automatically generated exampleJNI.java: class exampleJNI { public final static native void tryLong(int jarg1); } |