Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Calling Java from C and back?

Reply
Thread Tools

Calling Java from C and back?

 
 
Elena
Guest
Posts: n/a
 
      07-24-2009
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.


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-24-2009
On Fri, 24 Jul 2009 01:45:17 -0700 (PDT), Elena <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>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
 
Reply With Quote
 
 
 
 
Elena
Guest
Posts: n/a
 
      07-24-2009
On 24 Lug, 12:23, Roedy Green <(E-Mail Removed)>
wrote:
> On Fri, 24 Jul 2009 01:45:17 -0700 (PDT), Elena <(E-Mail Removed)>
> wrote, quoted or indirectly quoted someone who said :
>
> >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.
>
> 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.


 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      07-24-2009
On Fri, 24 Jul 2009, Elena wrote:

> 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
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      07-24-2009
Elena wrote:
> 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?


 
Reply With Quote
 
Elena
Guest
Posts: n/a
 
      07-24-2009
On 24 Lug, 17:49, "Mike Schilling" <(E-Mail Removed)>
wrote:
> Elena wrote:
> > 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
> > Jglobal
> > 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?


Because I'm calling Java from an executable, just to accomplish a
task.
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      07-24-2009
Elena wrote:
> On 24 Lug, 17:49, "Mike Schilling" <(E-Mail Removed)>
> wrote:
>> Elena wrote:
>>> 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
>>> Jglobal
>>> 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?

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java calling rsync calling ssh: io threads blocked pvbemmel-at-xs4all-nl Java 7 10-29-2010 08:38 AM
JNI C calling java with String gets null pointer in Java xingshan_he@hotmail.com Java 5 10-20-2005 08:25 AM
c++ calling java calling c++ ... Klaus Friese C++ 7 08-15-2005 09:23 PM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM
Urgent: Calling a method of a java object (getting a boolean parameter) from java script Eyal Javascript 2 08-07-2003 11:49 AM



Advertisments