by David Caldwell
7 January 2002
Revised 25 December 2002
Sun has a
"trail" as part of the (excellent)
Java Tutorial guide to
(Most parts of the Java Tutorial can also be
in book form. It seems as though other parts of the Java Tutorial can be purchased as books as well, but I haven't
We will use Cygwin tools to reproduce the "Hello World" example
created in the
Writing Java Programs With Native Methods
section of the JNI trail. It would probably be a good idea to read that trail along with this walkthrough.
The goal of this walkthrough will be to create a native method which does not rely on the Cygwin DLL. (In other
words, it will depend only on the Windows API.)
This walkthrough assumes that you already know how to program in Java, and also know how to program in C. In the case
of Java, the most important requirements are the ability to compile programs, execute programs, and deal with classpath
from Microsoft. Microsoft has an amazing ability to break links; if this link does not work, you might try
browsing from MSDN. You can also download something called the Platform
SDK, which probably contains the API documentation. In particular, the section on
Dynamic-Link Libraries: Also from Microsoft's site (and therefore also in constant danger of
becoming a broken link), and useful if you don't have a good background
in .DLLs and how they work.
Java Native Interface Specification:
From Sun; provides a comprehensive overview of the older JNI features. There are some new features that are described in
the JNI Enhancements in JDK 1.2
and JNI Enhancements in JDK 1.4 documents.
Java Native Interface Tips: From Sun's web site;
contains a couple of FAQ files and some links that may be useful if you want to do serious JNI (like
how to write for MacOS, for instance).
GNU Win32 related projects (Mumit Khan):
This page contains links to documentation about how to develop JNI with GNU tools on Windows, primarily
relating to the MinGW project. (I should point out that it is MinGW
which makes Cygwin's
-mno-cygwin flag -- upon which these tutorials depend -- work.)
SWIG (Simplified Wrapper and Interface Generator) is (according to its website):
"a software development tool that connects programs written in C, C++, and Objective-C with a variety of
high-level programming languages." According to SWIG's Java maintainer (also a Cygwin user), SWIG
"generates the JNI and Java code for calling C and C++ code from Java (as well as most scripting
languages). It creates Java proxy classes given a C/C++ header file."
Finding the Java-specific documentation on SWIG's
web site is a little tricky.
Although I have not used SWIG, it looks like
it would be very useful for projects which use heavy amounts of JNI. It appears that SWIG is biased
toward the object-oriented development paradigm (which I consider to be a Good Thing). For projects
with smaller Java-native integration needs, it appears SWIG might be overkill. Cool tech, though.
Questions on SWIG can be directed to the SWIG mailing list, at