Eric Bruno

Dr. Dobb's Bloggers

Java Communications Without JNI

December 20, 2013

PureJavaComm by Spare Time Labs is a pure Java alternative to RXTX — thanks to Al Williams for pointing this out to me.

I recently wrote about using the RXTX library to program an Arduino over the USB comms port. This sort of communication requires Java to native system access, and some don't like some of the complexities in getting RXTX to work. For instance, you need all of the right native libraries in the right place, and you need to set the library path on the Java command line just right. PureJavaComm by Spare Time Labs is a pure Java alternative to RXTX — thanks to Al Williams for pointing this out to me.

PureJavaComm's goal is to provide Java-only access to the comms port requiring only the Java Native Access (JNA) library. JNA is an open source project maintained by Tim Wall that allows you to write Java code only to call into native libraries (i.e., DLLs on Windows). Without JNA, you would have to write potentially complex Java Native Interface (JNI) code or other glue code to make this work. For instance, the following code sample from the JNA Wikipedia page works as-is on Windows, Linux, or OS X, to call the native C runtime printf function:

Let's take a look at how it works, and explore an example using it with an Arduino.

Inside JNA

JNA works similar to Microsoft's Platform Invocation Services, COM's IDispatch, or Java reflection/introspection. It implements a single small layer of JNI code that acts as a stub to call into any native library from your Java code. It runs on most platforms that support Java, maps all Java primitive types to native types, handles conversion between Java and C strings, supports unicode, variable arguments, Microsoft COM, and variable arguments. See the JNA project page for a complete list of features, but this is the bulk of them.

C pointers are mapped to Java through the use of arrays, as in this example:

Using PureJavaComm for Serial Port Communications

To begin with the PureJavaComm library, I downloaded the code from github, and opened the project in NetBeans, although it works in Eclipse as well. I chose the "Clean and build project" method, and NetBeans proceeded to use the Maven build scripts to download all the dependencies and then build the project seamlessly, and without issue.

To test it with an Arduino, I took the sample code and project from my previous blog on Arduino, and modified it slightly. First, instead of RXTX, I included only two JAR files in my build. The first is jna-4.0.0.jar, which is included with PureJavaComm in the lib directory. Second, I used the output of the PureJavaComm build, purejavacomm-0.0.21.jar, found in the bin directory of the project.

The updated Java-to-Arduino code is below. Here's a quick summary of the changes (marked in the comments as well):

Marked by "IMPORTS" in the comments, import the purejavacomms jtermios packages

Marked by "PORTS", I noticed a slight difference in the COMM port names returned

Marked by "CONTAINS", the difference in port names required this change

Marked by "FLOW-CONTROL", setting some flow control parameters are required

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!