I hate JNI

Discussion in 'Java' started by Brad, Aug 3, 2007.

  1. Brad

    Brad Guest

    So I have this C++ library I've written that is required to interface
    with Visual Basic and Java as well as be plugged into an existing C++
    application. Visual Basic wasn't that big of a deal, just a quick
    little VB wrapper and everything is great. C++, obviously, not a
    problem. The library functions perfectly -- PERFECTLY -- when called
    up from those two languages.

    Java, however...

    I wrote a JNI wrapper to wrap up and serve my library to Java. Most of
    the functionality of my library works just fine through JNI, except
    for one critical piece. I try to make a socket connection to a server
    from inside a call in the library at one point and my call to C++'s
    standard connect() function just...hangs. It sits there. It doesn't
    time out, it doesn't return, it just . . . hangs. I have no idea why.
    No error codes, no exceptions, no core dump. Nope. Just hangs. Like I
    said, it works just fine in C++ and in VB. But JNI seems to be working
    strangely with it.

    Maybe if I provide a little more information on that architecture of
    my library, someone can help me out. What happens is this: The user
    initializes a call to my library and makes a request. The request to
    the library returns some data to the user and, right before the
    request returns the data, a thread is split off in the library. This
    thread is a monitoring thread and needs to communicate with a server.
    The thread examines the request that was made and attempts a
    connection with the server.

    So, I'm wondering, is there something I'm missing with threading and
    JNI?

    Thanks
    Brad, Aug 3, 2007
    #1
    1. Advertising

  2. On Fri, 03 Aug 2007 22:30:25 -0000, Brad wrote:
    > I wrote a JNI wrapper to wrap up and serve my library to Java. Most
    > of the functionality of my library works just fine through JNI,
    > except for one critical piece. I try to make a socket connection to
    > a server from inside a call in the library at one point and my call
    > to C++'s standard connect() function just...hangs. It sits there. It
    > doesn't time out, it doesn't return, it just . . . hangs.


    Off hand I'd say that there is nothing special about using connect()
    from JNI, and that it can appear to hang for several minutes before
    failing even when called from a "regular" C program, due to the ways
    of TCP.

    Perhaps this is occuring because the arguments to the connect call are
    not what you expect, if they have passed through the layers from Java
    through JNI to your library with some datatype conversions along the
    way.

    How long have you waited?

    Have you observed the actual connection attempt with a tool like
    Wireshark?

    Post code, not a description of it.

    /gordon

    --
    Gordon Beaton, Aug 4, 2007
    #2
    1. Advertising

  3. On Fri, 03 Aug 2007 22:30:25 +0000, Brad wrote:

    > So I have this C++ library I've written that is required to interface
    > with Visual Basic and Java as well as be plugged into an existing C++
    > application.
    > Java, however...
    >
    > I wrote a JNI wrapper to wrap up and serve my library to Java. Most of
    > the functionality of my library works just fine through JNI, except
    > for one critical piece. I try to make a socket connection to a server
    > from inside a call in the library at one point and my call to C++'s
    > standard connect() function just...hangs.


    I recently wrote some C++ that java needs to call via JNI. I used SWIG to
    do the wrapping and built a standalone C++ program to test the routines
    that are wrapped via SWIG. This enabled me to debug it more easily and run
    valgrind on the standalone program. Only when it was fully working there
    did I really go to town on the java. Debugging C++ when invoked via JNI
    does seem really awkward to me. Once there was a memory corruption bug in
    the C++ and it made the java program blow up. The JVM said there was an
    internal error and produced a very detailed log. It was still easier to
    find and fix the problem from C++ though.
    --
    Andrew Marlow http://www.andrewpetermarlow.co.uk
    There is an emerald here the size of a plover's egg!
    Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
    Plain text mails only, please http://www.expita.com/nomime.html
    Andrew Marlow, Aug 4, 2007
    #3
  4. Brad

    Roedy Green Guest

    >standard connect() function just hangs. It sits there.
    At this point I would dump the parms to make sure you
    are passing exactly the same parms you do when
    you call from C++. Dump them in the C++ version
    and the Java version. Parameter passing is intricate in
    JNI, but you are in a perfectly ordinary C++ method
    on the other side of the barrier.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Aug 4, 2007
    #4
  5. On 04.08.2007 15:23, Roedy Green wrote:
    >> standard connect() function just hangs. It sits there.

    > At this point I would dump the parms to make sure you
    > are passing exactly the same parms you do when
    > you call from C++. Dump them in the C++ version
    > and the Java version. Parameter passing is intricate in
    > JNI, but you are in a perfectly ordinary C++ method
    > on the other side of the barrier.


    strace might also help and provide hints very quickly.

    robert
    Robert Klemme, Aug 5, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    846
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    901
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,682
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    618
    vasanth
    Jan 25, 2005
  5. bgabrhelik
    Replies:
    0
    Views:
    797
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page