I hate JNI

B

Brad

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
 
G

Gordon Beaton

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

--
 
A

Andrew Marlow

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.
 
R

Roedy Green

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.
 
R

Robert Klemme

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JNI Invocation API example 4
Using JNI Produced Jar Files: java.lang.UnsatisfiedLinkError 5
I hate you all 79
Trouble with JNI wrapper functions 1
JNI C++ Wrapper 1
JNI FindClass problem 3
JNI Programming 19
JNI 1

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top