What types of variables can be passed with JNI?

Discussion in 'Java' started by Ramon F Herrera, Jul 9, 2005.

  1. I have an application whose purpose is to open, display and rezise TIFF
    files.

    My application is written mainly in Java, but it uses C/C++ through
    JNI.

    I purchased a OCR library which receives calls form the Java side using
    _simple_ types of arguments such as int, floats and strings. For
    instance, to have a file analized I just pass its filesystem path (i.e.
    a string) to the C/C++ OCR library.

    My next version has to provide some extended functionality and I wonder
    if JNI is up to the task. More specifically, I would like to have
    resizing features similar to those provided by Acrobat: click on a plus
    sign and the image becomes bigger, type a percentage or move a sliding
    tool and the images is resized.

    After looking hard and long, it seems that the only libraries that can
    perform that resampling with an acceptable level of quality and speed
    are written in C/C++.

    Now the question: as said above, I know I can pass an int or a string
    between the Java side and the C/C++ side, but what about more complex
    variables? Can I open a file in Java and pass some sort of handle to
    it to the C library?

    What are the limitations of JNI in terms of passing "objects" back &
    forth?

    TIA.

    -Ramon F Herrera
    Ramon F Herrera, Jul 9, 2005
    #1
    1. Advertising

  2. On 8 Jul 2005 20:57:36 -0700, Ramon F Herrera wrote:
    > What are the limitations of JNI in terms of passing "objects" back &
    > forth?


    You can pass any Java types (primitives and object references) between
    Java and your JNI library. Once in a native method however, you can
    invoke any other C functions and pass whatever C data types you want.

    If you open a File in Java, yes you can pass the resulting object to a
    native method, however you will need to invoke Java methods from the
    native code in order to read from the file. If you want to read the
    file from C, consider opening it there as well (the java code can pass
    it the path as a String).

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jul 9, 2005
    #2
    1. Advertising

  3. Ramon F Herrera

    Roedy Green Guest

    On 8 Jul 2005 20:57:36 -0700, "Ramon F Herrera" <>
    wrote or quoted :

    >Now the question: as said above, I know I can pass an int or a string
    >between the Java side and the C/C++ side, but what about more complex
    >variables? Can I open a file in Java and pass some sort of handle to
    >it to the C library?


    You write glue code in C or C++ to tie the two together. That code
    calls your C methods to get work done. To talk to Java it uses a
    rather peculiar puppetry, manipulating java objects in the Java
    address space by remote control. You can set their fields, run their
    methods, all with a clumsy syntax. you can also construct new Java
    objects and pass parameters (e.g. C strings you have constructed) as
    their arguments.

    Either Java can call C, or slightly more complicated, C can call Java.

    I have written a short essay describing how it all works. See
    http://mindprod.com/jgloss/jni.html

    The trick to success is to try to keep the interactions between the
    sides to a minimum. Call C and STAY there as long as possible before
    returning. There is a grievous overhead for a call that goes over the
    Java/C boundary.

    In other words, don't call a C method 1000 times getting one int back
    each time. Instead call once and construct an array and pass that back
    as a lump.



    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jul 9, 2005
    #3
  4. Ramon F Herrera

    Roedy Green Guest

    On 8 Jul 2005 20:57:36 -0700, "Ramon F Herrera" <>
    wrote or quoted :

    > Can I open a file in Java and pass some sort of handle to
    >it to the C library?


    The C file handle is pretty useless in Java. What you would have to do
    is write C wrapper methods to do your io, and call them from Java.


    The easiest way to do the reverse would be the same, but you can
    manipulate a file or InputStream object remotely from C just as you
    can invoke the methods of any object.

    --
    Bush crime family lost/embezzled $3 trillion from Pentagon.
    Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
    http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

    Canadian Mind Products, Roedy Green.
    See http://mindprod.com/iraq.html photos of Bush's war crimes
    Roedy Green, Jul 9, 2005
    #4
  5. Ramon F Herrera

    Vit Guest

    Vit, Jul 15, 2005
    #5
    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. dee
    Replies:
    9
    Views:
    504
    Joseph Byrns
    Apr 15, 2005
  2. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    846
    Alex Hunsley
    Nov 4, 2003
  3. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    901
    Pasturel Jean-Louis
    Mar 3, 2004
  4. vasanth
    Replies:
    0
    Views:
    2,682
    vasanth
    Jan 25, 2005
  5. Anand
    Replies:
    2
    Views:
    897
    Anand
    Sep 11, 2003
Loading...

Share This Page