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.

132Chapter 5The Sun Microsystems Java Software <str<strong>on</strong>g>Development</str<strong>on</strong>g> Kit5.7GOING NATIVENow we come to the deeper darker mysteries. Let us take a look at javah. No,javah is not a Hebrew word. It is not some lost mystical text from the days beforescience supplanted magic. It is the Java C header and stub file generator.If you are not already fairly experienced in writing, compiling, and buildingshared libraries in C <strong>on</strong> a <strong>Linux</strong> system, we would suggest that you skip thissecti<strong>on</strong>, at least until you have developed intermediate skills in these areas.Otherwise, feel free to proceed.We’re going to walk you very quickly through building a Java nativemethod here. 9 D<strong>on</strong>’t worry if you d<strong>on</strong>’t quite follow it all. We will cover thistopic at greater length elsewhere in the book. For now, we’re giving you thehighlights. Also be sure to check out Secti<strong>on</strong> 5.15. We’ll point you to manyadditi<strong>on</strong>al resources <strong>on</strong> JNI (Java Native Interface) in that secti<strong>on</strong>.Sounds pretty intimidating, huh? Well, depending up<strong>on</strong> your backgroundand experience, it can be a bit intimidating. As much as this will hurt some diehardJava purists, Java is not the right language for everything. Java’s size andsemiinterpreted nature in particular make Java ill-suited for the “close to themetal” tasks, such as device drivers and raw socket networking.Fortunately, Java’s designers were of this rocket-scientist breed (and so,for that matter, are your bending authors), so they gave Java programmers aback door: native methods. A native method is a class method whose name, arguments,and return type are declared in Java, but whose underlying implementati<strong>on</strong>is written in “native code” (usually C, but it could be any compiledlanguage that can match C’s stack frame c<strong>on</strong>venti<strong>on</strong>s).As an example, let’s implement a native method that will use the native<strong>Linux</strong> C library calls to get the current program’s effective user ID and thename associated with that user.First, we will write the Java class (Example 5.7).You may never have seen code like that at the start of a class definiti<strong>on</strong>.The block declaredstatic { ... }9. You might be tempted to call our comments in the introducti<strong>on</strong> where we menti<strong>on</strong>ed thatwe did not like purely pedagogical examples and that we would provide real, useful code. Well,we have to c<strong>on</strong>fess that there are some features of the Java language that we couldn’t cram intoour real-world examples. This JNI sample is <strong>on</strong>e such. We admit our failure, and we apologize.

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

Saved successfully!

Ooh no, something went wrong!