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
paul.h.burns - 23 Jul 2007 17:47 GMT
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
Roedy Green - 24 Jul 2007 03:05 GMT
On Mon, 23 Jul 2007 09:47:39 -0700, "paul.h.burns"
<paul.h.burns@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>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

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Thomas Fritsch - 23 Jul 2007 17:54 GMT
> 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
[quoted text clipped - 5 lines]
> 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")

Signature
Thomas
Thomas Fritsch - 23 Jul 2007 23:08 GMT
[...]
> 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)

Signature
Thomas
Roedy Green - 24 Jul 2007 02:58 GMT
>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

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com