Do you want to access native library methods without requiring any additional JNI or native code ?
At times, there are instances you would need to use native (non-Java) codes (e.g., C/C++) to overcome the memory management and performance constraints in Java. Though Java does support native codes via the Java Native Interface (JNI) it is difficult when it comes to cross-platform mappings and mappings for a number of commonly used platform functions, mappings for all primitive data types, etc.,
In this post, we look at the JNA library that allows you to call directly into native functions using natural Java method invocation.
Quick Snapshot
#1.Overview
JNA provides your Java programs access to native shared libraries without requiring JNI or native code. Native access functionality is provided by the libffi library. When the Native
class is first accessed, JNA will first attempt to load the required libraries from the directories specified in jna.boot.library.path
. If that fails and jna.nosys=false
is set, it will fall back to loading from the system library paths. Finally, it will attempt to extract the stub library from the JNA jar file, and load it.
JNA is capable of extracting and loading the native library on its own, so you don’t need additional configuration. JNA falls back to extraction if the native library is not already installed on the local system somewhere accessible to System.loadLibrary
. The native library is also available in platform-specific jar files for use with Java Web Start. Begin by downloading the latest release of JNA and referencing jna.jar
 in your project’s CLASSPATH
.
JNA provides a Java interface to describe functions and structures in the target native library. This makes it easy to take advantage of native platform features without incurring the high overhead of configuring and building JNI code for multiple platforms.
JNA has support for Linux-like platforms, most probably if your platform is supported by libffi, then chances are you can build JNA for it. Pre-built platform support may be found here.
Below is a JNA example where it maps the printf
function from the standard C library and calls it.
package com.sun.jna.examples; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.Platform; /** Simple example of JNA interface mapping and usage. */ public class HelloWorld { // This is the standard, stable way of mapping, which supports extensive // customization and mapping of Java to native types. public interface CLibrary extends Library { CLibrary INSTANCE = (CLibrary) Native.load((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class); void printf(String format, Object... args); } public static void main(String[] args) { CLibrary.INSTANCE.printf("Hello, World\n"); for (int i=0;i < args.length;i++) { CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]); } } }
#2.Key Features
- Automatic mapping from Java to native functions, with simple mappings for all primitive data types.
- Automatic conversion between C and Java strings, with customizable encoding/decoding.
- Auto-generated Java proxies for native function pointers.
- Type-safety for native pointers.
- Optimized direct mapping for high-performance applications.
- Support for
- Java array and NIO Buffer arguments (primitive types and pointers) as pointer-to-buffer
- Nested structures and arrays
- Wide (wchar_t-based) strings
- Native long support (32- or 64-bit as appropriate)
- Structure and Union arguments/return values, by reference and by value
- Function Pointers
#3.How to Get it
- Download core artifact of JNA and contains only the binding library and the core helper classes – jna-5.3.1.jar
- Cross-platform mappings and mappings for a number of commonly used platform functions – jna-platform-5.3.1.jar
#4.Additional Resources
- Any questions related to post it to jna-users Google group
- JNA reference is here.
- Check out example applications
- Use JNAerator utility to auto-generate a library mapping
Average Rating