Calling Java from C and back?

E

Elena

Hi,

I've developed a native application which calls Java by means of JNI
and everything is going fine. What I'd like to do now is collecting
debug logs from the Java side together with logs from the main native
application. That is, I have to call C from Java.

It doesn't seem that Java allows simple callbacks from Java to C. The
lame solution I'm going to employ if I fail in finding a simpler
solution is to build a JNI DLL which stores C callbacks in some global
variable and then gets loaded from Java, therefore being able to call
those C callbacks.

Any suggestion?

Thanks.
 
R

Roedy Green

It doesn't seem that Java allows simple callbacks from Java to C.

On the Java side you need a delegate object and on the C side you
could strip that down to a simple callback.

see http://mindprod.com/jgloss/delegate.html
http://mindprod.com/jgloss/callback.html

Also consider you division of labour. C is best for platform specific
stuff. Java is best for bookkeeping. Perhaps it would be easier to
move your code more to the Java side or vice versa to avoid the
impedance problem.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"The industrial civilisation is based on the consumption of energy resources that are inherently limited in quantity, and that are about to become scarce. When they do, competition for what remains will trigger dramatic economic and geopolitical events; in the end, it may be impossible for even a single nation to sustain industrialism as we have know it in the twentieth century."
~ Richard Heinberg, The Party’s Over: Oil, War, and the Fate of Industrial Societies
 
E

Elena

On the Java side you need a delegate object and on the C side you
could strip that down to a simple callback.

seehttp://mindprod.com/jgloss/delegate.htmlhttp://mindprod.com/jgloss/callback.html

Also consider you division of labour.  C is best for platform specific
stuff. Java is best for bookkeeping.  Perhaps it would be easier to
move your code more to the Java side or vice versa to avoid the
impedance problem.

My situation is different: I have a legacy native application which I
have to add a new feature to, and such a feature is readily available
in an open source Java library. Everything works fine, but I'd prefer
to add logging of Java side to the native logging infrastructure. It
is just a matter of sending strings back to the C side.

Thanks.
 
T

Tom Anderson

I've developed a native application which calls Java by means of JNI and
everything is going fine. What I'd like to do now is collecting debug
logs from the Java side together with logs from the main native
application. That is, I have to call C from Java.

It doesn't seem that Java allows simple callbacks from Java to C.

Right. Java can only call specific named native functions, not arbitrary
ones identified only by a function pointer.
The lame solution I'm going to employ if I fail in finding a simpler
solution is to build a JNI DLL which stores C callbacks in some global
variable and then gets loaded from Java, therefore being able to call
those C callbacks.

Any suggestion?

Pass the function pointers to the callbacks to java as integers (or
longs). Create a native method which is a callback caller: java can call
it, passing in a function pointer and some parameters, and it will call
them. Does that make sense?

This is similar to what you're suggesting, i think, but it moves the data
storage to the java side, where it should be easier.

tom

--
Formal logical proofs, and therefore programs - formal logical proofs
that particular computations are possible, expressed in a formal system
called a programming language - are utterly meaningless. To write a
computer program you have to come to terms with this, to accept that
whatever you might want the program to mean, the machine will blindly
follow its meaningless rules and come to some meaningless conclusion. --
Dehnadi and Bornat
 
M

Mike Schilling

Elena said:
Hi,

I've developed a native application which calls Java by means of JNI
and everything is going fine. What I'd like to do now is collecting
debug logs from the Java side together with logs from the main
native
application. That is, I have to call C from Java.

It doesn't seem that Java allows simple callbacks from Java to C.
The
lame solution I'm going to employ if I fail in finding a simpler
solution is to build a JNI DLL which stores C callbacks in some
global
variable and then gets loaded from Java, therefore being able to
call
those C callbacks.

Any suggestion?

Why not model the C callbacks as (native) methods on a Java class, and
use JNI the usual way?
 
E

Elena

Why not model the C callbacks as (native) methods on a Java class, and
use JNI the usual way?

Because I'm calling Java from an executable, just to accomplish a
task.
 
M

Mike Schilling

Elena said:
Because I'm calling Java from an executable, just to accomplish a
task.

Right; I mean the calls from Java back to C. Is the issue that the
callbacks are located in the executable, where your JNI DLL will have a hard
time seeing them? If so, you can set an array of function pointers in the
DLL (at runtime, from your C main()), and let the native methods call them.
You can even throw Java exceptions from the native methods if the function
pointer array hasn't been set.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top