Using Native Methods in your Java Code

Yep, we can include our old code written in some other language into now-patronized Java. Here we tell you a simple, clean way of integrating C code with a Java program. Could we expect a favour? Please do not tell anyone from Sun that you were smooshing code in this way, for it might get us a load of unsolicited mail trying to size us up about the way we write programs. But wait up.

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...

There. We have a small-is-sweet C program integrated with Java! So what's the fuss all about ? Admittedly, this procedure might have its shortcomings, but isn't it a better way to embark on a new concept than groping with CLASSPATH and javah ?

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


Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Bombay 400 034, India.
http://www.neca.com/~vmis
e-mail : vmukhi@giasbm01.vsnl.net.in
1