This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes I'm looking for JNI real world examples Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "I Watch "I New topic
Author

I'm looking for JNI real world examples

Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
I've been looking around the net for quite a while on real world
JNI examples. I am really getting tired of all the Hello,World examples that I keep finding. What I am looking for is how to get at C structures from Java code. I would like to build a JNI wrapper around an existing C Library but I cannot find enough good information beyond Hello,World to get me started. Does anyone have any good links to some tutorials which go beyond Hello,World using Java's Native Interface?
I am using JDK 1.3.
Thanks,
Frank
Harish Kashyap
Ranch Hand

Joined: Jun 14, 2000
Posts: 118
There is a very good example of jni that we use in jdbc applications. that is the use of jdbc-odbc bridge.
the jdbc accesses the odbc methods or the resources through the jni(native methods).
If u could explore these native methods u'll explore the real application use of jni.
the jdbc drivers and classes are available with jdk. use reflection to explore it.
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Do you have the C code for the JDBC-ODBC bridge? The part which implements the native calls?
I wanna know how I map C structures to Java objects.
Frank
Amit Zzz Kulkarni
Ranch Hand

Joined: Jul 05, 2000
Posts: 30
Frank,
What exactly do you mean by this ? I assume that you want to pass the structures to Java using JNI ,if so store your C structs in some memory allocated with malloc() or if using C++ an object allocated by new. Then cast the pointer to this (C or C++) object to a jint and return the jint to your java code to be used as a native structure handle.

Then when ever the java code calls back to your C or C++ code pass the jint handle to the C code and cast it back to the C object and use it.
If you are running on a system with 64 bit pointers (not likely) then use a jlong instead of a jint. Or just use a jlong to start with so that you can upgrade easily to 64 bits in the future.
Originally posted by Frank Hale:
What I am looking for is how to get at C structures from Java code. I would like to build a JNI wrapper around an existing C Library
Amit Zzz Kulkarni
Ranch Hand

Joined: Jul 05, 2000
Posts: 30
Or you can convert ALL your structs to Java classes and then map them,member by member.Please ask a specific question in future so somebody may help you out faster.
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Next time I will be more clear.
Okay what I am doing is trying to create a binding to a C lib (Xlib - X Windows System). This is a huge library and very complex. I've figured out how to cast some pointers to jint and pass them back to java as a handle. I've created a few native wrapper functions around Xlib functions which then cast the jint back to the original type, struct, or whatever. I am having a problem with XEvent as its a union and when I try to cast it, it will not fit inside a jint or a jlong. If I try to pass a reference and then cast that back I crash the VM everytime.
I've probably went into to much detail. Can anyone help? I can post code if you like.
Another question if I was to create java objects for C structs how would I go about passing the data to the java object?
Frank
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by Frank Hale:
Next time I will be more clear.
Okay what I am doing is trying to create a binding to a C lib (Xlib - X Windows System). This is a huge library and very complex. I've figured out how to cast some pointers to jint and pass them back to java as a handle. I've created a few native wrapper functions around Xlib functions which then cast the jint back to the original type, struct, or whatever. I am having a problem with XEvent as its a union and when I try to cast it, it will not fit inside a jint or a jlong. If I try to pass a reference and then cast that back I crash the VM everytime.
I've probably went into to much detail. Can anyone help? I can post code if you like.
Another question if I was to create java objects for C structs how would I go about passing the data to the java object?
Frank

I haven't tried it but, one solution I heard of is to create a new C DLL that converts to types that do map to java so esentually your java program calls a DLL which calls the DLL that you need. In the case of a Union, send it back as seperate elements. (Which is basically what a union is anyway, seperate types pointing to the same location in memory)

[This message has been edited by Carl Trusiak (edited July 12, 2000).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230

I haven't tried it but, one solution I heard of is to create a new C DLL that converts to types that do map to java so esentually your java program calls a DLL which calls the DLL that you need. In the case of a Union, send it back as seperate elements. (Which is basically what a union is anyway, seperate types pointing to the same location in memory)

small problem with this Union

That is the way its defined in Xlib.h and now you typically do this:
XEvent ev;
And then pass ev to functions like so:
XNextEvent(display, window, &ev);
The problem is how do I know what value to pass back to java and when?
This is becoming a very difficult issue because nowhere have I found information on JNI interfacing C code with Java which talks about what to do in these situations.
Frank
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Okay I figured out how to pass the XEvent pointer over to java as a handle and now I can perform event loops in Java instead of in pure C. If anyone wants to see the magic I performed let me know.
Thanks,
Frank
Amit Zzz Kulkarni
Ranch Hand

Joined: Jul 05, 2000
Posts: 30
Frank,
This site is no good in sending notifications that this question was updated. I am glad that you solved it and am interested in how you got it ?
Regards
Amit
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Well I allocated memory for the C struct using calloc then passed the value of the pointer back to Java and stuff it in an int. Then when I need to make other native calls from java I just pass back this value (which I call a handle) and then cast it to its type in the C native method. I use the finalize() method in java to free the allocated memory.
Frank
chickmath vidyanand
Greenhorn

Joined: Oct 06, 2003
Posts: 1
Could you please send me the whole stuffs like C source code and Java code.
Actually I am also in need of the same requirements like passing structures (data) from C++ to JAVA.
Please send me some simple samples so that I can understand easily.
It should have C++ program having structure defined(should contain data). Then this data should be passed to JAVA and printed in JAVA program.
Can anyone knows how to do this, please help me....
Urgent requirement...
rgds,
Vidyanand
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

cagri sener,
Your post was moved to a new topic.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
Amit Zzz Kulkarni wrote:Frank,
This site is no good in sending notifications that this question was updated.


You can set things in your Profile, or click ~'Watch Topic' at the bottom.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Jesus Angeles wrote:
Amit Zzz Kulkarni wrote:Frank,
This site is no good in sending notifications that this question was updated.


You can set things in your Profile, or click ~'Watch Topic' at the bottom.
If he has waited 11 years for a notification of a reply, he is a very patient person
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41065
    
  43
You can set things in your Profile, or click ~'Watch Topic' at the bottom.

That works now, but neither option existed when Amit was around.


Ping & DNS - my free Android networking tools app
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2049
My bad. I didnt notice that it was posted in 2000.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
You can tell from his middle name Zzz which proves he has slept nearly as long as Rip van Winkle.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I'm looking for JNI real world examples
 
Similar Threads
JNI examples
Importing a value to Java from C/C++ using JNI
JNI Resources
JNI - how do I access a C strucure from Java?
How can I get CPU-ID in java?