For a complete JNI documentation and specification see SUN's JNI page: http://java.sun.com/docs/books/jni/html/jniTOC.htmlThis lesson walks you through the steps necessary to integrate native code with programs written in Java.
This lesson implements the canonical "JavaDriverJNI.java" program. The "JavaDriverJNI.java" program has one Java class, called JavaDriverJNI.java does two things:
- it declares TWO native methods that process String Arrays and Integer Arrays, and
- it implements the
mainmethod for the overall package. The implementation for the native method is provided in C++.
Note : This lesson assumes that you are starting with neither existing C++ functions nor Java classes. While "in the real world" you probably have existing C/C++/Fortran functions that you wish to integrate with Java programs, you will still need to modify the signatures for these external C++ functions to work with the JNI. To be sure that you use the correct signatures, it is best to begin by first writing and compiling the Java code, as described here.
Writing native methods for Java programs is a multi-step process.
mainmethod which calls the native method.
javahwith the native interface flag
-jni. Once you've generated the header file you have the formal signature for your native method.
The following figure illustrates these steps for the JavaDriverJNI.java program:
Create a Java class named JavaDriverJNI that declares all native methods. This class also includes a
mainmethod that creates an object of type JavaDriverJNI and calls the native methods, as needed.
javacto compile the Java code that you wrote in Step 1.
javahto create a JNI-style header file (a
.hfile) from the JavaDriverJNI class. The header file provides a function signature for the implementation of the native methods
processArray (these are defines in two different C++ files, see below).
Write the implementations for the native methods in native language (such as ANSI C, C++, Fortran) source files (CPPArrayFilterJNI.cpp & CPPArrayFilterJNI.cpp). The implementations will be regular functions (processArray & processString, respectively) that are integrated with your main Java class, JavaDriverJNI.
Use the C++ (or other) compiler to compile the header
.hfiles and the source
.cfiles that you created in Steps 3 and 4 into shared libraries. In Windows 95/NT terminology, a shared library is called a dynamically loadable library (DLL), in Unix these are .so files.
And finally, use
javaJavaDriverJNI, the Java interpreter, to run the program.
2. You can also start the JVM and instantiate Java objects from within native methods (written in say C/C++). For detailed description and examples see this page: http://java.sun.com/docs/books/jni/html/invoke.html.javac JavaDriverJNI.javajavah -jni JavaDriverJNIbcc32 -I"C:\Program Files\JavaSoft\include" -IC:\Borland\BCC55\Include -LC:\Borland\BCC55\Lib -I"C:\Program Files\JavaSoft\include\win32" -WD CPPArrayFilterJNI.cpp -oCPPArrayFilterJNI.dllbcc32 -I"C:\Program Files\JavaSoft\include" -IC:\Borland\BCC55\Include -LC:\Borland\BCC55\Lib -I"C:\Program Files\JavaSoft\include\win32" -WD CPPStringFilterJNI.cpp -oCPPStringFilterJNI.dlljava JavaDriverJNI arg1 arg2 ARG3pause