What types of variables can be passed with JNI?

  • Thread starter Ramon F Herrera
  • Start date
R

Ramon F Herrera

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
 
G

Gordon Beaton

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
 
R

Roedy Green

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
 
R

Roedy Green

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
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top