Can we offer a miniature observation here (with due apologies to the women). Ever realized that next to standing in the rain with pneumonia, trusting a woman during the day is the most dangerous thing to do? Whether young or old, they are born with that femme fatale instinct of putting their best foot forward...both literally as well as metaphorically. Just about as well as the back of their palms, they know what dress to put on and when, how to dab the make-up to look the best in all lights and how to make you-know-what out of every men. The best time to catch them is, therefore, not during the day (especially in its latter part) but at the time they have just woken up. That is the real beauty, and it is easier to love the made-up woman once you have seen what she looks like when she is herself.
The simple example that we use here to introduce the concept to the people might not be the best and the most efficient way to embed code, but IT WORKS. And its a good start. If you don't agree with us, try the original Sun's Tutorial on Native Methods . Another point to be noted is that this example is a standalone program that we chose to use as our first illustration because applets seem to be too ornery when it comes to working without fuss. (That reminds us, we will soon put up some standalone programs as well). And unfortunately, we have also used Visual C++ 4.0 / 2.1 for the purpose of creating DLLs. No need to start day-dreaming, you will soon know what a DLL (Dynamic Link Library) is all about.
Here we go...
class aaa { public static void main(String s[]) { bbb a; System.out.println("Start"); a = new bbb(); System.out.println("Before xx"); a.xx(); System.out.println("After xx"); } } class bbb { public native void xx(); public bbb() { System.out.println("in zzz"); System.loadLibrary("zzz"); } }
We have an ordinary class called aaa , where we have our main() , very much reminiscent of the good old C/C++. An object that looks like String is passed along with it. Remember argv[] ? Our string s[] is identical to that. Now the word static . It could very well have been free, because that is what static means. Lets get it cleared. Ordinarily, whenever we have to use a function of a class, we have to first initialize an object that looks like that class and then refer the function through the object. But if we make our main() into a static function, we can use it without having to initialize an object to aaa .
Inside the main() , we have an object a which looks like our class bbb . The System.out.println() takes care of printing on to our monitor, and in that sense, is the cousin of printf. So the string start will be printed on the screen. Then we initialize a to bbb and print Before xx . Through a , we call the xx() of class bbb .
In the class bbb , we have our statement public native void xx() . People who have tasted C or C++, particularly the ones who have a soft corner for computer terminology, might mistake this statement for a function prototype. But it is not so. One thing that ought to be crystal clear in our minds is that Java does not have prototypes . In fact, without the word native , we would have got an error in the above line. The word native tells the compiler that the function following it is one written in some other language (in our case C). Again, System.loadLibrary() picks up a DLL file in to memory and the alien code that we have to use/call has to be in the form of a .dll file, not .exe . So compile the above program with the usual javac jane.java .
Note that the function here has to be called by a special name. It has to be prefixed with Java_ and suffixed with _stub . The middle part has to be the class name ( in our case bbb ) and the function name (in our case xx ) seperated by another underscore (_). The function needs a void pointer to be returned so that is the return type of the function. There are two parameters -- both void pointers p and q , but since we are not using them, let us not confuse ourselves with their exact employment. (If you are too adamant, though, p is like the this pointer). For the sake of simplicity, not to mention the stability of our heads, we ensure that the function performs some simple task. Hence, we just use the ever-so-popular printf to display a statement that says It works . And then hope it does :)void *Java_bbb_xx_stub(void * p, void * q) { printf("It works! \n"); return p; }
This is because if you do not have the Java_bbb_xx_stub given under exports, then it cannot be called by any other external program.LIBRARY ZZZ EXPORTS Java_bbb_xx_stub @1
Start in zzz Before zzz It works! After zzz
Nevertheless, it does not mean we will overlook the butterfly if we like the caterpillar. Very soon, we will see how to use it in those intricate ways as well...
Do you like this quick and dirty (but undeniably understandable) way of learning Java? Feel free to let us know what you feel about it.
Find out about the Cyberconference on How to teach Java or just scamper back to
The Java page