Writing native methods involves importing C code into your Java application. In this tip I'll walk you through the basic recipe for creating native methods and using them in a Java application.
Seven steps to native method nirvana The steps to creating native methods are as follows:
* Write Java code
* Compile Java code
* Create C header (.h file)
* Create C stubs file
* Write C code
* Create shared code library (or DLL)
* Run application
To use native methods in your Java code, you must do two things. First, write a native method declaration for each native method that you want to use. This is just like writing the declaration of a normal Java method interface, but you must specify the native keyword, as follows:
public native void printText ();
The second hoop to jump through is you must explicitly load the native code library. (We will create this later.) We do this by loading the library in a class static block:
static
{
System.lo adLib rary ("happy");
}
To put these pieces together for our example, create a file called Happy.java with the following contents:
class Happy
{
public native void printText ();
static
{
System.loadLib rary ("happy"); /* Note lowercase of classname! */
}
public static void main (String[] args)
{
Happy happy = new Happy ();
happy.printText ();
}
}
For more visit :
http://www.javaworld.com/ja vawor ld/javatips/jw-javatip23.html
Answered by
Ni ..
, an ibibo Master,
at
8:38 PM on July 13, 2008