swig and java

View: New views
4 Messages — Rating Filter:   Alert me  

swig and java

by Saloustros Georgios :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello all.
I am using swing in order to wrap c code to call it from a java program.
My question is this: How can I tell swig when i execute the command swig -java <interface file> to declare tha generated java classes into  a package rather tha default?
For example I want the generated java classes to look like this :
packege server/*name of the package*/
public class MyclassJNI{....}

The reason to do that is tha at did at first manually ( i opened the myclassJNI.java file and i added the package server declaration ) but then at runtime,
although the .so library is loaded i get an UnsatsfiedLinkError exception.
Thanx,
GS

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: swig and java

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Saloustros Georgios wrote:

> Hello all.
> I am using swing in order to wrap c code to call it from a java program.
> My question is this: How can I tell swig when i execute the command swig
> -java <interface file> to declare tha generated java classes into  a
> package rather tha default?
> For example I want the generated java classes to look like this :
> packege server/*name of the package*/
> public class MyclassJNI{....}
>
> The reason to do that is tha at did at first manually ( i opened the
> myclassJNI.java file and i added the package server declaration ) but
> then at runtime,
> although the .so library is loaded i get an UnsatsfiedLinkError exception.
> Thanx,
> GS
>

http://www.swig.org/Doc1.3/Java.html. Search for 'package'.


William

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: swig and java

by andyq :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi 2 all,

I need help for a specific problem which I can not resolve by my own.
I have to access from Java to an c library where I have the header file and the library.
The main problem is that I do not get back a parameters value which is in the c world a void*

So for now my example.i file looks like
%module example
%{
        #include "my_headerfile.h"
%}
%include "my_headerfile.h"


my_headerfile.h looks like:
...

typedef void* HANDLE;

...
extern int myFunction( HANDLE* Handle, unsigned short Port, char* IpAddr, .... );

If I now generate the proxy with SWIG I receive the following wrapper:

SWIGEXPORT jint JNICALL Java_exampleJNI_myFunction(JNIEnv *jenv, jclass jcls, jlong jarg1, jint jarg2, jstring jarg3, jstring jarg4, jstring jarg5, jint jarg6, jstring jarg7, jint jarg8, jint jarg9) {
  jint jresult = 0 ;
  HANDLE *arg1 = (HANDLE *) 0 ;
  unsigned short arg2 ;
  char *arg3 = (char *) 0 ;
  char *arg4 = (char *) 0 ;
  char *arg5 = (char *) 0 ;
  int arg6 ;
  char *arg7 = (char *) 0 ;
  int arg8 ;
  int arg9 ;
  int result;

  (void)jenv;
  (void)jcls;
  arg1 = *(HANDLE **)&jarg1;
  arg2 = (unsigned short)jarg2;
  arg3 = 0;
  if (jarg3) {
    arg3 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg3, 0);
    if (!arg3) return 0;
  }
  arg4 = 0;
  if (jarg4) {
    arg4 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg4, 0);
    if (!arg4) return 0;
  }
  arg5 = 0;
  if (jarg5) {
    arg5 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg5, 0);
    if (!arg5) return 0;
  }
  arg6 = (int)jarg6;
  arg7 = 0;
  if (jarg7) {
    arg7 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg7, 0);
    if (!arg7) return 0;
  }
  arg8 = (int)jarg8;
  arg9 = (int)jarg9;

  result = (int)myFunction(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9);

  jresult = (jint)result;
  if (arg3) (*jenv)->ReleaseStringUTFChars(jenv, jarg3, (const char *)arg3);
  if (arg4) (*jenv)->ReleaseStringUTFChars(jenv, jarg4, (const char *)arg4);
  if (arg5) (*jenv)->ReleaseStringUTFChars(jenv, jarg5, (const char *)arg5);
  if (arg7) (*jenv)->ReleaseStringUTFChars(jenv, jarg7, (const char *)arg7);

  return jresult;
}

// Side issue ---
So know I always get a exception because arg1 ( is the void* HANDLE ) is given as value
result = (int)myFunction(arg1,arg2,arg3,arg4,arg5,...

and not by reference
result = (int)myFunction(&arg1,arg2,arg3,arg4,arg5,...
// End side issue

If I change that ( with & ) then the call works and I can call from my java code the C library but the parameter jarg1 should give back the value from the pointer of the arg1 and that is not working.

I do not get back the value of the pointer arg1 into my java world.

The SWIG generated exampleJNI.java JNI class looks like:
public final static native int myFunction(long jarg1, int jarg2, String jarg3, String jarg4, String jarg5, int jarg6, String jarg7, int jarg8, int jarg9);

And the example.java looks like:
public static int myFunction(SWIGTYPE_p_p_void Handle, int Port, String IpAddr, String User, ... ) {
    return exampleJNI.myFunction(SWIGTYPE_p_p_void.getCPtr(Handle), Port, IpAddr, User, ... );
  }

Does anyone see here what's wrong? Because I need the handle value back in my java world....

Looking forward to hear some interesting solution from you
andy!

Re: swig and java

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

andyq wrote:

> Hi 2 all,
>
> I need help for a specific problem which I can not resolve by my own.
> I have to access from Java to an c library where I have the header file and
> the library.
> The main problem is that I do not get back a parameters value which is in
> the c world a void*
>
> So for now my example.i file looks like
> %module example
> %{
>         #include "my_headerfile.h"
> %}
> %include "my_headerfile.h"
>
>
> my_headerfile.h looks like:
> ...
>
> typedef void* HANDLE;
>
> ...
> extern int myFunction( HANDLE* Handle, unsigned short Port, char* IpAddr,
> .... );
>
> If I now generate the proxy with SWIG I receive the following wrapper:
>
> SWIGEXPORT jint JNICALL Java_exampleJNI_myFunction(JNIEnv *jenv, jclass
> jcls, jlong jarg1, jint jarg2, jstring jarg3, jstring jarg4, jstring jarg5,
> jint jarg6, jstring jarg7, jint jarg8, jint jarg9) {
>   jint jresult = 0 ;
>   HANDLE *arg1 = (HANDLE *) 0 ;
>   unsigned short arg2 ;
>   char *arg3 = (char *) 0 ;
>   char *arg4 = (char *) 0 ;
>   char *arg5 = (char *) 0 ;
>   int arg6 ;
>   char *arg7 = (char *) 0 ;
>   int arg8 ;
>   int arg9 ;
>   int result;
>
>   (void)jenv;
>   (void)jcls;
>   arg1 = *(HANDLE **)&jarg1;
>   arg2 = (unsigned short)jarg2;
>   arg3 = 0;
>   if (jarg3) {
>     arg3 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg3, 0);
>     if (!arg3) return 0;
>   }
>   arg4 = 0;
>   if (jarg4) {
>     arg4 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg4, 0);
>     if (!arg4) return 0;
>   }
>   arg5 = 0;
>   if (jarg5) {
>     arg5 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg5, 0);
>     if (!arg5) return 0;
>   }
>   arg6 = (int)jarg6;
>   arg7 = 0;
>   if (jarg7) {
>     arg7 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg7, 0);
>     if (!arg7) return 0;
>   }
>   arg8 = (int)jarg8;
>   arg9 = (int)jarg9;
>
>   result = (int)myFunction(arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9);
>
>   jresult = (jint)result;
>   if (arg3) (*jenv)->ReleaseStringUTFChars(jenv, jarg3, (const char *)arg3);
>   if (arg4) (*jenv)->ReleaseStringUTFChars(jenv, jarg4, (const char *)arg4);
>   if (arg5) (*jenv)->ReleaseStringUTFChars(jenv, jarg5, (const char *)arg5);
>   if (arg7) (*jenv)->ReleaseStringUTFChars(jenv, jarg7, (const char *)arg7);
>
>   return jresult;
> }
>
> // Side issue ---
> So know I always get a exception because arg1 ( is the void* HANDLE ) is
> given as value
> result = (int)myFunction(arg1,arg2,arg3,arg4,arg5,...
>
> and not by reference
> result = (int)myFunction(&arg1,arg2,arg3,arg4,arg5,...
> // End side issue
>
> If I change that ( with & ) then the call works and I can call from my java
> code the C library but the parameter jarg1 should give back the value from
> the pointer of the arg1 and that is not working.
>
> I do not get back the value of the pointer arg1 into my java world.
>
> The SWIG generated exampleJNI.java JNI class looks like:
> public final static native int myFunction(long jarg1, int jarg2, String
> jarg3, String jarg4, String jarg5, int jarg6, String jarg7, int jarg8, int
> jarg9);
>
> And the example.java looks like:
> public static int myFunction(SWIGTYPE_p_p_void Handle, int Port, String
> IpAddr, String User, ... ) {
>     return exampleJNI.myFunction(SWIGTYPE_p_p_void.getCPtr(Handle), Port,
> IpAddr, User, ... );
>   }
>
> Does anyone see here what's wrong? Because I need the handle value back in
> my java world....
>
> Looking forward to hear some interesting solution from you
> andy!
If you are getting your handle back by passing arg1 by reference, it
won't necessarily work on all JVMs as the parameters in the JNI method
are usually passed by value.

The portable solution is to use the approach taken by the OUTPUT
typemaps documented in Java.html. However, there is no OUTPUT typemap
for void **, so I suggest you take one from typemaps.i for another type
and modify. Alternatively, if you don't feel up to typemaps, just write
and wrap a helper function that returns the HANDLE by value, or return a
struct containing the HANDLE and the int return value.

William


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user