InOnIt.com
Cygwin/Java
Calling C from Java
Organizing for JNI
Writing the HelloWorld class
Creating a .h file
Writing the C function
Loading the DLL into the JVM
Running the program
NEW!World Football Rankings & World Cup Odds
David's Company

Structure of a JNI Program

JNI really consists of two parts -- the ability to implement Java methods using native code, and the "Invocation API," which allows a native application to embed the Java VM. We're going to focus on the first approach, so our programs will start as Java programs which use native code to implement some methods.

Native methods can be static or non-static. Since our Hello World method will be very procedural, we'll go ahead and make it static.

The general steps required to implement our example will be:

  • Create a Java class with a native method, and compile it,
  • Use the javah tool to generate a C header file for the class,
  • Write the C code implementing the method, compile it, and link it into a DLL,
  • Insert code into our Java program telling it to load the DLL,
  • Enable our Java program to find the DLL,
  • Execute our Java program,
  • Read Hello World in the console window.