This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

confused with native reserve word

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i need to know every thing about reserve word native in java ,
its usage , its advanteges & every thing right from the scratch.
I'll be much pleased if little piece of code is aslo added to make clear picture of "native" in my mind.
thanks in advance.
------------------
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this will help you some.
Calling a native method
We�ll start with a simple example: a Java program that calls a native method, which in turn calls the standard C library function printf( ).

The first step is to write the Java code declaring a native method and its arguments:

//: appendixb:ShowMessage.java
public class ShowMessage {
private native void ShowMessage(String msg);
static {
System.loadLibrary("MsgImpl");
// Linux hack, if you can't get your library
// path set in your environment:
// System.load(
// "/home/bruce/tij2/appendixb/MsgImpl.so");
}
public static void main(String[] args) {
ShowMessage app = new ShowMessage();
app.ShowMessage("Generated with JNI");
}
} ///:~
The native method declaration is followed by a static block that calls System.loadLibrary( ) (which you could call at any time, but this style is more appropriate). System.loadLibrary( ) loads a DLL in memory and links to it. The DLL must be in your system library path. The file name extension is automatically added by the JVM depending on the platform.

In the above code you can also see a call to the System.load( ) method, which is commented out. The path specified here is an absolute path, rather than relying on an environment variable. Using an environment variable is naturally the better and more portable solution, but if you can�t figure that out you can comment out the loadLibrary( ) call and uncomment this one, adjusting the path to your own directory.
The header file generator: javah
Now compile your Java source file and run javah on the resulting .class file, specifying the �jni switch (this is done automatically for you by the makefile in the source code distribution for this book):

javah �jni ShowMessage
javah reads the Java class file and for each native method declaration it generates a function prototype in a C or C++ header file. Here�s the output: the ShowMessage.h source file (edited slightly to fit into this book):

This is from appendix B of "Thinking in Java". It is available for free download at www.bruceeckel.com
[This message has been edited by Randall Twede (edited January 24, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic