|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The following Javacode segment defines a class namedHelloWorld. This class declares one native method, implements a main method, and has a static code segment.
class HelloWorld { public native void displayHelloWorld(); static { System.loadLibrary("hello"); } public static void main(String[] args) { new HelloWorld().displayHelloWorld(); } }
You must declare all methods, whether Java methods or native methods, within a class on the Java side. When you write a method implementation in a language other than Java, you must include the keywordnativeas part of the method's definition within the Java class. Thenativekeyword signals to the Java compiler that the function is a native language function. It is easy to tell that the implementation for theHelloWorldclass'sdisplayHelloWorldmethod is written in another programming language because thenativekeyword appears as part of its method definition:This native method declaration in your Java class provides only the method signature forpublic native void displayHelloWorld();displayHelloWorld. It provides no implementation for the method. You must provide the implementation fordisplayHelloWorldin a separate native language source file.The method declaration for
displayHelloWorldalso indicates that the method is a public instance method, accepts no arguments, and returns no value. For more information about arguments to and return values from native methods see Interacting with Java from the Native Side.
You compile the native language code that implementsdisplayHelloWorldinto a shared library (you will do this in Step 5: Create a Shared Library). The runtime system later loads the shared library into the Java class that requires it. Loading the library into the Java class maps the implementation of the native method to its declaration.The
HelloWorldclass uses theSystem.loadLibrarymethod. TheSystem.loadLibrarymethod loads the shared library that will be created when you compile the implementation code. Place this method within a static initializer. The argument toSystem.loadLibraryis the shared library name. This can be any name that you choose. The system uses a standard, but platform-specific, approach to convert the library name to a native library name. For example, the Solaris system converts the library name "hello" tolibhello.so, while a Microsoft Windows system converts the same name tohello.dll.The following static initializer from the
HelloWorldclass loads the appropriate library, namedhello. The runtime system executes a class's static initializer when it loads the class.static { System.loadLibrary("hello"); }
TheHelloWorldclass, because it is an application, also includes amainmethod to instantiate the class and call the native method. Themainmethod instantiatesHelloWorldand calls thedisplayHelloWorldnative method.You can see from the code sample that you call a native method in the same manner as you call a regular method: just append the name of the method to the end of the object name, separated with a period ('.'). A matched set of parentheses, (), follow the method name and enclose any arguments to pass into the method. Thepublic static void main(String[] args) { new HelloWorld().displayHelloWorld(); }displayHelloWorldmethod doesn't take any arguments.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.