In Java programming we need to implement some native methods. You must be wondering about what's native methods.
Javah - Header File Generator
In Java programming we need to implement some native
methods. You must be wondering about what's native methods.
Firstly, The native methods are in pure C code, not C++. The function prototypes
are in an object-oriented form of C which are being provided by javah , but they are still not object methods.
Secondly, We can call native methods applications only. However due to
some security reasons, we cannot call applets from native methods.
Thirdly, native methods are platform-specific. This is the most
important point to remember, you have to build a dynamically loadable library to
link your java application with the native operating system (Windows OS,
Machintosh, Linux, Unix ..). For each native platform your application
targets, a dynamically loadable library is needed to be shipped.. That means any system-specific code has to be
ported along with the java code.
On the other hand, native methods are the only way to use any system features not provided by the Java Virtual Machine.
To implement these methods Javah generates C header and source
files that are used by C programs to reference an Object's instance variables
from native source code. The name of the header file and the structure declared within it are derived from the name of the class.
By default javah creates a header file for each class listed on the command line and puts the files in the current directory. As stated above the name of the header file is derived from the name of the class. If any class inside the package is passed to javah, the package name gets prepended to both the header file name and the structure name.
Following are some options to use :
-o outputfile
This option concatenates the resulting header or source files for all the classes listed on the command line into
output file.
-help
Print help message for javah usage.
-d directory
This option sets the directory where javah saves the header files or the stub files. Only one of -d or -o may be used.
-classpath path
Specifies the path javah used to look up classes. Overrides the default or the
CLASSPATH environment variable if it is set. Directories are separated by semi-colons. Thus the general format for path is:
.;<your_path>
For example:
.;C:\users\dac\classes;C:\tools\java\classes
-stubs
Causes javah to generate C declarations from the Java object file.
-verbose
Indicates verbose output and causes javah to print a message to stdout concerning the status of the generated files.
-version
Prints out javah version information.