Can't understand something of JNI

C

cold80

Today I've tried my first application with JNI. I made a simple Java
class with a native method, I generated the C stub with javah and
finally I wrote the implementation of the C function. Everythink works
just fine. In the implementation C file I had of course to include the
file jni.h. What I can't understand is where the JNI code is
written....I mean, where is the code that I can use to convert types
(like GetStringLength for example)? I could not understand very well
the content of the file jni.h but it doesn't seem to contain the real
code, just typedefs and structure definitions. I didn't link any
library into my VS2005 project, so I can't understand where the code
for the JNI functions is located.

Thank you in advance for your help

Cold
 
P

paul.h.burns

I'm learning JNI myself, though using JNIWrapper from Teamdev. From my
nascent understanding of the situation, I believe what you're looking
for (functions such as GetStringLength) are JNI functions already
implemented in the JDK.

Hope that helps,
Paul
 
T

Thomas Fritsch

cold80 said:
Today I've tried my first application with JNI. I made a simple Java
class with a native method, I generated the C stub with javah and
finally I wrote the implementation of the C function. Everythink works
just fine. In the implementation C file I had of course to include the
file jni.h. What I can't understand is where the JNI code is
written....I mean, where is the code that I can use to convert types
(like GetStringLength for example)? I could not understand very well
the content of the file jni.h but it doesn't seem to contain the real
code, just typedefs and structure definitions.
Correct!
Note that the structure definitions contain mostly function pointers.
I didn't link any library into my VS2005 project,
When Java calls your C function you get the "env" pointer.
From that "env" you get a pointer to the JNI function (pointing back
somewhere into Sun's C libraries):
(*env)->GetStringLength
and then you call the function lying at the pointed place:
(*env)->GetStringLength(...);
Note that "GetStringLength" is just a name inside a struct (handled by
the compiler), not an external function name (like "printf", handled by
the linker). Hence there is no need for any library to get your DLL linked.
so I can't understand where the code
for the JNI functions is located.
To satisfy your curiosity: The JNI functions are in one of the native
libraries coming with the JRE (on Windows: in "java.dll")
 
T

Thomas Fritsch

Thomas Fritsch wrote:
[...]
To satisfy your curiosity: The JNI functions are in one of the native
libraries coming with the JRE (on Windows: in "java.dll")
.... or in "jvm.dll"
(as the MSVC-debugger told me when stepping into the disassembled code)
 
R

Roedy Green

Today I've tried my first application with JNI. I made a simple Java
class with a native method, I generated the C stub with javah and
finally I wrote the implementation of the C function. Everythink works
just fine. In the implementation C file I had of course to include the
file jni.h. What I can't understand is where the JNI code is
written

see http://mindprod.com/jgloss/jni.html

Also try downloading some of my code that uses JNI and see how it fits
together. e.g.

http://mindprod.com/products1.hml#FILETIMES
 
R

Roedy Green

I'm learning JNI myself, though using JNIWrapper from Teamdev. From my
nascent understanding of the situation, I believe what you're looking
for (functions such as GetStringLength) are JNI functions already
implemented in the JDK.

they live in #include <jni.h>
Javah generates the include.

You must also have
J:\Program Files\Java\jdk1.6.0_02\include\
and
J:\Program Files\Java\jdk1.6.0_02\include\win32
in the include list e.g.
in Tools | options | Projects and Solutions | VC++ directories |
include files
or
in tools | options | directories | include
For project as a whole:
In project | settings | general | no MFC
In project | settings | link | output filename | should end in DLL
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top