![]() ![]() ![]() ![]() |
Implementing Native Methods |
The example implements a simple "character-replace" program. You invoke the program with these command line arguments:Thechar1 char2 inputfile outputfileReplace
program reads from inputfile, replaces all occurrences of char1 with char2, and writes the results to outputfile.The Source Files
- Replace.java
- Contains the main program.
- File.java
- Defines the File class. This class provides basic file and path manipulation with the expectation that subclasses will provide the actual file management code depending on the file semantics they want to present. InputFile and OutputFile both derive from File.
- InputFile.java
- Contains the InputFile class (a subclass of File) that implements a read-only input file. This class declares three native methods whose implementations are written in the C programming language and provided in
InputFileImpl.c
.- OutputFile.java
- Contains the OutputFile class (a subclass of File) that implements a write-only output file. This class declares three native methods whose implementations are written in the C programming language and provided in
OutputFileImpl.c
.Files Generated by
javah
- File.h
- InputFile.h
- OutputFile.h
- C header files generated by
javah
.- File.c
- InputFile.c
- OutputFile.c
- C stub files generated by
javah -stubs
.Instructions
- Compile the
.java
files into.class
files using the Java compiler- Compile all of the C code into a dynamically loadable library named "file". If you don't know how to do this, follow the instructions in Step 6: Create a Dynamically Loadable Library
in the Step By Step lesson.
- Run the program using the Java interpreter.
![]() ![]() ![]() ![]() |
Table of Contents |