Native Methods

Although it is rare, occasionally, you may want to call a subroutine that is written in a language other than Java. Typically, such a subroutine exists as executable code for the CPU and environment in which You are working-s-that is, native code. For example, you may want to call a native code subroutine to achieve faster execution time. Or, you may want to use a specialized, third-party library, such as a statistical package. However, because java programs are compiled to byte code, which is then interpreted (or compiled on-the-fly) by the Java run-time system, it would seem impossible to calla native code subroutine from within your java program. Fortunately, this conclusion is false. Java provides the native keyword, which is used to declare native code methods; Once declared, these methods can be called from inside your java program just es you call any other Java method.

To declare a native method, precede the method with the native modifier, but do not define any body for the method. For example Publican native McIntosh () ; After you declare a native method, you must write the native method and follow a rather complex series of steps to link it with your Java code. Most native methods are written in C. The mechanism used to integrate C code ' with a Java program is called the Java Native interface (JNI). This methodology was created by Java 1.1 arid then expanded and enhanced by java 2. (Java 1.0 used a different approach, which is now completely outdated.) A detailed description of the .IN! is beyond the scope of this book, but the following description provides sufficient information for most applications The easiest way to understand the process is to work through nil example. To begin, enter the following short program, which uses a native method called test( ):

Notice that the test() method is declared as native and has no body. This is the method that we will implement in C shortly: Also notice the static block. As explained earlier in this book, a static block is executed only once, when your program begins execution ' (or, more precisely, when its class is first loaded). In this case, it is used to load the dynamic link library that contains the native implementation of test( ).(You will see how to create this library soon.) . The' Library is loaded by the Deflationary ) method, which is'part of the System class. This is its general for static void l d Library (Instrumentalist)

Here,file name is a string that s~es the name of the file that holds the library. For the Windows 95/98/NT environment, this me is assumed to have the DLL extension After you enter the program, compile it to produce Alive Demo class. Next, you' , must use javah.exe to produce one file: Demonstrative. (javah.exe is included in the JDK;) You will Include.Overemotional in your implementation of test( ). To produce Demonstrative; use the following command This command produces a header, me called Desdemona. This me must be.included in the C file that implements testy). The output produced by this command is shown here Pay specialization to the foll~wing line, which defines the prototype for the .test( ).function that you create Notice that the name of the function is Java_Native Demo_test( ). You must use this as the name of the native function that you implement. That is,'instead.of creating a C function called ,test( ), you create one called Java..Native Demo_test ).

The Native Demo component of the prefix is added because it identifies the test( ) methods being part of the Native Demo class. Remember., another class may define its own native test( ) method u,at is completely different from the one declared by' , Native Demo conclude the class name in the prefix provide a way to differentiate between differing slovens. As a general rule, native functions will be given a name . whose prefix includes the name of the class in which they are declared. , After thudding necessary header file, you can write your implementation of test( ) and store it in a file named Native Demo.c:

Notice that this file include which contains interfacing information -.This file is provided by your Java compiler. Titleholder file Native Demo.h was created by java . In this function, the. Get Object ass( '> method is used to obtain a structure that has information about the class Native Demo. The Get Field lD( ) method returns a C structure with information about When field named. for the class. Greenfield{) retrieves the original value of that field ..Unwieldiness() stores an updated value-in that field. (See the file jili.h for additional methods that handle- other types of data.) . After creating Native Demo.c, you must compile it and.create a DLL this by using the Microsoft C/C++ compiler, use the following command line: This produces a file called Nalive Demo.dll. Once this is done, you can execute the Java program, which will produce the following output:

Tile specifics surrounding tile use of nature implementation- and interdependent. Furthermore, the specific manlier. in which you interface to Java code is subject to change  must consult the documentation that accompaniment Java . development system for details on native methods

Share This