Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

What types of variables can be passed with JNI?

Thread view: 
Ramon F Herrera - 09 Jul 2005 04:55 GMT
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
Gordon Beaton - 09 Jul 2005 08:29 GMT
> 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

Signature

[  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

Vit - 15 Jul 2005 05:37 GMT
There is another alternatice to JNI- xFunction, which is some easier in usage.
Have a look at
http://www.excelsior-usa.com/xfunction.html

Vit
Roedy Green - 09 Jul 2005 09:57 GMT
>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.

Signature

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 - 09 Jul 2005 13:47 GMT
>  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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.