Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can, or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing heavily in Java's future as a platform for platforms

Also see:

Discuss: Java: A platform for platforms?

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Open source Java projects: Java Native Access

An easier way to access native code

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

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 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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (2)
Login
Forgot your account info?

ThankyouBy JavaJeff1 on November 10, 2008, 2:29 amThankyou for your kind words regarding my article. I don't have any additional JNA tutorials at the moment, but could provide some on my website (http://javajeff.mb.ca)...

Reply | Read entire comment

More examples on JNABy Anonymous on November 5, 2008, 5:01 amI'm really impressed with this article as it's really simplified, well understanding short tutorial and it's better than the tutorial published on the project's...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources