Previous | Next | Trail Map | Integrating Native Methods into Java Programs | Table of Contents


A Comprehensive Example

This lesson in under construction. However, we are making the source code for the example available for you to look at now. By reading the source and the comments within the code, you should be able to figure out the example. In addition, you should be able to figure out how to return values from and pass values into native methods, and about some of the helper functions and macros provided by the Java development environment that provide the connection between the Java language and C.

The Example

The example consists of a simple "character-replace" program. You invoke the program with these command line arguments:
char1 char2 inputfile outputfile
The Replace 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
Contains the File superclass that provides 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.java
Contains the InputFile class (a subclass of File) that implements a read-only input file.
Note: This class has several native methods whose implementations are written in the C programming language.
OutputFile.java
Contains the OutputFile class (a subclass of File) that implements a write-only output file.
Note: This class has several native methods whose implementations are written in the C programming language.
file.c
Contains the C implementations for the native methods defined by both the InputFile and OutputFile classes.

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

  1. Compile the .java files into .class files using the Java compiler
  2. 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.
  3. Run the program using the Java interpreter.


Previous | Next | Trail Map | Integrating Native Methods into Java Programs | Table of Contents