Calling Java from C and back?

Discussion in 'Java' started by Elena, Jul 24, 2009.

  1. Elena

    Elena Guest

    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.
     
    Elena, Jul 24, 2009
    #1
    1. Advertising

  2. Elena

    Roedy Green Guest

    On Fri, 24 Jul 2009 01:45:17 -0700 (PDT), Elena <>
    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
     
    Roedy Green, Jul 24, 2009
    #2
    1. Advertising

  3. Elena

    Elena Guest

    On 24 Lug, 12:23, Roedy Green <>
    wrote:
    > On Fri, 24 Jul 2009 01:45:17 -0700 (PDT), Elena <>
    > 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.
     
    Elena, Jul 24, 2009
    #3
  4. Elena

    Tom Anderson Guest

    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
     
    Tom Anderson, Jul 24, 2009
    #4
  5. 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?
     
    Mike Schilling, Jul 24, 2009
    #5
  6. Elena

    Elena Guest

    On 24 Lug, 17:49, "Mike Schilling" <>
    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.
     
    Elena, Jul 24, 2009
    #6
  7. Elena wrote:
    > On 24 Lug, 17:49, "Mike Schilling" <>
    > 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.
     
    Mike Schilling, Jul 24, 2009
    #7
    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. Joby
    Replies:
    0
    Views:
    1,662
  2. sylvia sil
    Replies:
    1
    Views:
    651
    Curt_C [MVP]
    Dec 29, 2004
  3. Klaus Friese

    c++ calling java calling c++ ...

    Klaus Friese, Aug 12, 2005, in forum: C++
    Replies:
    7
    Views:
    613
    Roland Pibinger
    Aug 15, 2005
  4. =?Utf-8?B?Z29yaWxsYQ==?=
    Replies:
    1
    Views:
    820
    Masudur
    May 25, 2007
  5. pvbemmel-at-xs4all-nl

    java calling rsync calling ssh: io threads blocked

    pvbemmel-at-xs4all-nl, Sep 20, 2010, in forum: Java
    Replies:
    7
    Views:
    1,460
    pvbemmel-at-xs4all-nl
    Oct 29, 2010
Loading...

Share This Page