Kalpana Kalpana (Editor)

Java Native Access

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Development status
  
Active

Written in
  
C and Java

Original author(s)
  
Todd Fast, Timothy Wall, Liang Chen

Initial release
  
May 9, 2007 (2007-05-09)

Stable release
  
4.3.0 / January 14, 2017; 52 days ago (2017-01-14)

Operating system
  
Windows, OS X, Android, AIX, FreeBSD, Linux, OpenBSD, Solaris, Windows Mobile

Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort. No boilerplate or generated glue code is required.

Contents

Architecture

The JNA library uses a small native library called foreign function interface library (libffi) to dynamically invoke native code. The JNA library uses native functions allowing code to load a library by name and retrieve a pointer to a function within that library, and uses libffi library to invoke it, all without static bindings, header files, or any compile phase. The developer uses a Java interface to describe functions and structures in the target native library. This makes it quite easy to take advantage of native platform features without incurring the high development overhead of configuring and building JNI code.

JNA is built and tested on Mac OS X, Microsoft Windows, FreeBSD / OpenBSD, Solaris, Linux, AIX, Windows Mobile, and Android. It is also possible to tweak and recompile the native build configurations to make it work on most other platforms that run Java.

Mapping types

The following table shows an overview of types mapping between Java and native code and supported by the JNA library.

Note: The meaning of TCHAR changes between char and wchar_t according to some preprocessor definitions. LPCTSTR follows.

Memory byte alignment for data structures

Native libraries have no standardized memory byte alignment flavor. JNA defaults to an OS platform specific setting, that can be overridden by a library specific custom alignment. If the alignment details are not given in the documentation of the native library, the correct alignment must be determined by trial and error during implementation of the Java wrapper.

Example

The following program loads the local C standard library implementation and uses it to call the printf function.

Note: The following code is portable and works the same on Windows and Linux / Unix / Mac OS X platforms.

The following program loads the C POSIX library and uses it to call the standard mkdir function.

Note: The following code is portable and works the same on POSIX standards platforms.

The program below loads the Kernel32.dll and uses it to call the Beep and Sleep functions.

Note: The following code works only on Windows platforms.

Adoption

Java Native Access is known to be used in:

  • Armed Bear Common Lisp uses JNA for its CFFI implementation
  • JRuby use JNA for POSIX functionality
  • Freedom for Media in Java (FMJ)
  • IntelliJ IDEA IDE by the company JetBrains
  • OpenSearchServer an open source search engine and web crawler
  • SVNKit pure Java Subversion client library with optional native library integration through the JNA.
  • Videolan vlcj Java Multimedia Library.
  • Cyberduck FTP, SFTP, WebDAV, Cloud Files & Amazon S3 Browser for Mac OS X.
  • Log4j, a library of native log appenders (http://log4jna.codeplex.com).
  • Hudson and Jenkins continuous integration servers.
  • Webdriver
  • YAJSW (Yet Another Java Service Wrapper)
  • Cassandra, an open source distributed database from the Apache Software Foundation uses JNA
  • References

    Java Native Access Wikipedia