13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.7 Going Native135is an 8-bit encoding for Unicode that coincides with ASCII in the low 7 bits,so it is often used for such c<strong>on</strong>versi<strong>on</strong>s). You can think of the JNIEnv as a C++class pointer, or you can think of it as a structure of data and functi<strong>on</strong> pointers(that’s really what a C++ class is, after all). The bottom line is, it provides themeans to access and manipulate the Java envir<strong>on</strong>ment from your native code.The sec<strong>on</strong>d argument, jobject, is the “this” pointer. It points to theGetUser class, and it is upcast to the JNI equivalent of the Java Objecttype. If our method took parameters, they would follow these two c<strong>on</strong>stantarguments.JNI is a huge topic. You can read more about it in the Sun MicrosystemsJNI Tutorial, 11 or in Java 2 SDK JNI FAQ, 12 or in the JNI 1.1 Specificati<strong>on</strong>, 13or in the associated JDK 1.2 Update 14 or the JDK 1.4 Update. 15Even with all of this “We’re too busy to explain things to you” going <strong>on</strong>here, we’ve got a lot more to cover before we are d<strong>on</strong>e. The next step in ourlittle 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 sec<strong>on</strong>dcommand makes a shared library out of it. Now, refer back to the static initializerin 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 theSystem class to load the shared library we just created. Note that library namingrules of the target OS are applied. The library is named getuser and <strong>on</strong>a <strong>Linux</strong> system it is assumed that that library will be in a file namedlibgetuser.so.The last line sets an envir<strong>on</strong>ment variable, LD_LIBRARY_PATH, to providea path where Java will search for libraries. This is behavior inherited from Solaris.<strong>Linux</strong> uses ldc<strong>on</strong>fig to maintain a list of shared libraries. Usually, a libraryis placed in a directory named in the file ld.so.c<strong>on</strong>f and a memory cache of11. http://java.sun.com/docs/books/tutorial/native1.1/index.html12. http://java.sun.com/products/jdk/faq/jni-j2sdk-faq.html13. http://java.sun.com/products/jdk/1.2/docs/guide/jni/spec/jniTOC.doc.html14. http://java.sun.com/j2se/1.4.1/docs/guide/jni/jni-12.html15. http://java.sun.com/j2se/1.4.1/docs/guide/jni/jni-14.html

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!