Open source Java projects: Java Native Access

An easier way to access native code

If you've used the Java Native Interface (JNI) to make a platform-specific native library accessible to your Java programs, you know how tedious it can be. Jeff Friesen continues his series on lesser-known open source Java projects by introducing you to Java Native Access -- a project that eliminates the tedium and error associated with JNI and lets you access C libraries programmatically.

In situations where Java does not provide the necessary APIs, it is sometimes necessary to use the Java Native Interface (JNI) to make platform-specific native libraries accessible to Java programs. For instance, I have used JNI to access universal serial bus and TWAIN-based scanner device libraries on the Windows XP platform, and a smartcard library on an older Windows NT platform.

Open source licenses
Like the balloontip project introduced last month, JNA is subject to the GNU Lesser General Public License (LGPL) v2.1. See the Resources section to learn more about this open source license.

In all cases I have followed the same basic (and tedious) procedure: First I created a Java class to load a JNI-friendly library (that accesses the other library) and declare its native methods. After using the JDK's javah tool to create prototypes for the JNI-friendly library's functions from the class's native method declarations, I wrote the library in the C language and compiled the code with my C compiler.

Although there is nothing difficult about these tasks, writing the C code is slow work -- C-based string manipulation and working with pointers can be tricky, for instance. Furthermore, it is easy to make a mistake when using JNI, which can lead to memory leaks and program crashes whose causes are hard to detect.

In this second article in the Open source Java projects series, I'll introduce you to an easier and safer solution: Todd Fast and Timothy Wall's Java Native Access (JNA) project. JNA lets you access native code from Java while avoiding C and the Java Native Interface. I'll start with a quick introduction to JNA and the software required to run the examples in this article. After that, I'll show you how to use JNA to port useful code from three Windows native libraries into your Java programs.

Get started with JNA

The Java Native Access project is hosted on, where you can download the project's online Javadoc and the software itself. Although the download section identifies five JAR files, you only need to download jna.jar and examples.jar for the purposes of this article.

JNA and Java Web Start
Along with jna.jar and examples.jar, the JNA project download section presents three JAR files that let you work with JNA in a Java Web Start context on Mac OS X, Linux/Unix, and Windows platforms.

The jna.jar file provides the essential JNA software and is required to run all of the examples you'll find here. This JAR file contains several packages of classes, along with JNI-friendly native libraries for the Unix, Linux, Windows, and Mac OS X platforms. Each library is responsible for dispatching native method calls to native libraries.

The examples.jar file provides various examples that illustrate JNA's usefulness. One of them uses JNA to implement an API for specifying transparent windows on the various platforms. In this article's final example, I'll show you how to use this API to fix the transparency problem identified by the VerifyAge2 application in last month's article.

1 2 3 4 5 Page 1