Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

I hate JNI

Thread view: 
Brad - 03 Aug 2007 23:30 GMT
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
Gordon Beaton - 04 Aug 2007 09:06 GMT
> 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

--
Andrew Marlow - 04 Aug 2007 11:54 GMT
> 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++
[quoted text clipped - 6 lines]
> 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.
Signature

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

Roedy Green - 04 Aug 2007 14:23 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Robert Klemme - 05 Aug 2007 18:59 GMT
>> standard connect() function just hangs. It sits there.
> At this point I would dump the parms to make sure you
[quoted text clipped - 3 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.