Contents Prev Next

4 Generating Header Files


4.1 - Header File for the InputFile class
4.2 - Header File for the OutPutFile class
In order to manipulate Java objects from C you need to generate an appropriate C header declaration that conforms to the layout of your Java class. This can be done by running javah on your class and including the resultant header file in your C code.

To get the C header file you need to compile your Java code with javac. Once compiled, you run your compiled class through javah to generate the header file. Note that when you run javah, you pass javah the class name, not the file name.

By default, javah produces a subdirectory named CClassHeaders and places your header files in there. In CClassHeaders, javah saves a C-style header file for each class it is run on. The name of the header file and the structure that is declared are derived from the name of the class.*1

The structure generated by javah maps to the data portion of an Java object. The HandleTo() macro is a "magic" macro that defines the HDemo_InputFile and the HDemo_OutPutFile data structures. These structures are marshalled in by the Java stubs at runtime.

In order to get to the data portion of the Java object within your C function, you use unhand() on the HDemo variable. You can think of the HDemo pointer in the native methods as the "this" pointer in C++.


Header File for the InputFile class

/* Header for class demo_InputFile */

#ifndef _Included_demo_InputFile
#define _Included_demo_InputFile
typedef struct Classdemo_InputFile {
    struct Hjava_lang_String *path;
#define demo_InputFile_separatorChar 58
    long fd;
} Classdemo_InputFile;

HandleTo(demo_InputFile);
extern /*boolean*/ long demo_InputFile_open(struct Hdemo_InputFile *);
extern void demo_InputFile_close(struct Hdemo_InputFile *);
extern long demo_InputFile_read(struct Hdemo_InputFile *,HArrayOfByte *,long);
#endif

Header File for the OutPutFile class

/* Header for class demo_OutputFile */

#ifndef _Included_demo_OutputFile
#define _Included_demo_OutputFile
typedef struct Classdemo_OutputFile {
    struct Hjava_lang_String *path;
#define demo_OutputFile_separatorChar 58
    long fd;
} Classdemo_OutputFile;

HandleTo(demo_OutputFile);
extern /*boolean*/ long demo_OutputFile_open(struct Hdemo_OutputFile *);
extern void demo_OutputFile_close(struct Hdemo_OutputFile *);
extern long demo_OutputFile_write(storuct Hdemo_OutputFile *,HArrayOfByte *,long);
#endif
Contents Prev Next

Implementing Native Methods

Generated with CERN WebMaker