Using The Java Native Interface
In this tutorial we illustrate how the Java Native Interface (JNI) can be used to permit
Java programs to communicate with C programs. We begin by compiling the Java
program and the C program at the command prompt (i.e. outside of Eclipse) using the
MinGW gcc compiler. Once the programs are compiled and executing correctly, we
show how the programs can be integrated into an Eclipse project using the CDT plugin.
Whatever the reason for using it, the JNI bridges the gap between Java and C. It does this
by accessing shared libraries that can be written in C (or possibly C++). We first write
our Java code, identifying certain methods as belong to such a library. Then we write our
C code and compile it into a library. We can then run our Java code as usual, and it will
transparently access the library and run the native function.
The remainder of the code resembles standard Java code. An instance of the Hello class
is created, and one of its methods, sayHello() is called. The fact that the method is native
is irrelevant to the caller. Simply compile Hello.java as you would for any other Java
source file (using javac Hello.java). The result will be a file called Hello.class.
Source: www.cs.umanitoba.ca
Related PDF Files
Topic:
Comments for Using The Java Native Interface