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.
The Java Native Access project is hosted on Java.net, 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.
| Subject |
|
|
jna.jar and examples.jar from the JNA downloads page.
JNABy breakthedawn on October 2, 2009, 10:31 amHi Jeff thxs in advance for your help. I try to use a foreign library which provides a method "int myMethod(MODULE_ITEM** arg)". "MODULE_ITEM" contains different...
Reply | Read entire comment
jna how convert CallBack FunctionBy Anonymous on August 10, 2009, 4:37 amI have a project using JNA.The customer give me a API.The API contain a CallBack Function(typedef int (_stdcall *LP_NETX_CALLBACKFUNC)(unsigned int uServiceCode,char*...
Reply | Read entire comment
JNA to acess DCOMBy Anonymous on May 27, 2009, 11:40 amHi Jeff? A lot of JNA topics focus on system dll. My newbie question is, can JNA be used to call DCOM methods? 'Cause I have created a DCOM and I don't want to re-write...
Reply | Read entire comment
ThanksBy javajeff on March 24, 2009, 8:54 pmI'm glad you enjoyed my article. For additional JNA material, please check out my More JNA Examples article, which is hosted on my website. Jeff "JavaJeff" Friesen
Reply | Read entire comment
FollowupBy javajeff on March 24, 2009, 8:53 pmI've written a small More JNA Examples article for my website. This article provides a small tutorial on JNA's callbacks (function pointers) feature. I plan to provide...
Reply | Read entire comment
View all comments