Java Reference
In-Depth Information
is an 8-bit encoding for Unicode that coincides with ASCII in the low 7 bits,
so it is often used for such conversions). You can think of the JNIEnv as a C++
class pointer, or you can think of it as a structure of data and function pointers
(that's really what a C++ class is, after all). The bottom line is, it provides the
means to access and manipulate the Java environment from your native code.
The second argument, jobject , is the “this” pointer. It points to the
GetUser class, and it is upcast to the JNI equivalent of the Java Object
type. If our method took parameters, they would follow these two constant
arguments.
JNI is a huge topic. You can read more about it in the Sun Microsystems
JNI Tutorial, 11 or in Java 2 SDK JNI FAQ, 12 or in the JNI 1.1 Specification, 13
or in the associated JDK 1.2 Update 14 or the JDK 1.4 Update. 15
Even with all of this “We're too busy to explain things to you” going on
here, we've got a lot more to cover before we are done. The next step in our
little demo is to compile the C program and create a shared library of the code.
$ cc -c GetUser.c
$ cc -shared -o libgetuser.so GetUser.o
$ export LD_LIBRARY_PATH=.
The first line compiles the native method to a .o (object) file. The second
command makes a shared library out of it. Now, refer back to the static initial-
izer in Example 5.7. A static initializer is run before everything else in a class,
even before main() . In this case, it uses the loadLibrary() method of the
System class to load the shared library we just created. Note that library naming
rules of the target OS are applied. The library is named getuser and on
a Linux system it is assumed that that library will be in a file named
libgetuser.so .
The last line sets an environment variable, LD_LIBRARY_PATH , to provide
a path where Java will search for libraries. This is behavior inherited from So-
laris. Linux uses ldconfig to maintain a list of shared libraries. Usually, a library
is placed in a directory named in the file ld.so.conf and a memory cache of
11. http://java.sun.com/docs/books/tutorial/native1.1/index.html
12. http://java.sun.com/products/jdk/faq/jni-j2sdk-faq.html
13. http://java.sun.com/products/jdk/1.2/docs/guide/jni/spec/jniTOC.doc.html
14. http://java.sun.com/j2se/1.4.1/docs/guide/jni/jni-12.html
15. http://java.sun.com/j2se/1.4.1/docs/guide/jni/jni-14.html
Search WWH ::




Custom Search