• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

I'm looking for JNI real world examples

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Frank Hale
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cagri sener,
Your post was moved to a new topic.
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 75842
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jesus Angeles
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad. I didnt notice that it was posted in 2000.
 
Campbell Ritchie
Marshal
Posts: 75842
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can tell from his middle name Zzz which proves he has slept nearly as long as Rip van Winkle.
 
reply
    Bookmark Topic Watch Topic
  • New Topic