Java Foreign Function Interface

| by Bienvenido David Follow 0 Followers on Mar 17, 2014. Estimated reading time: 3 minutes |

JDK Enhancement Proposal (JEP) 191 defines the Java Foreign Function Interface (FFI), which are interfaces that can bind native functions to Java methods and can manage blocks of native memory. This JEP will make it easier to add new native-level features to the JDK and help provide a standard FFI for use in Java development. Java Native Interface (JNI) is the current standard programming interface for writing Java native code. JNI is not a simple way to create native functions, and this gave birth to libraries like Java Native Access (JNA) and Java Native Runtime (JNR). Both JNA and JNR are built on top of JNI, and JEP 191 will likely be based on JNR.

JEP 191 provides a Foreign Function Interface at the Java level, similar to JNA or JNR. It will allow easy access to native functions, and will manage native memory at the JVM level. It will also support a future JSR for a standard Java FFI. The Java FFI API can be used for implementing native-backed features like NIO, advanced file system metadata, and process management. Using the FFI API will be the preferred way to bind native code and memory, instead of JNI. JEP 191 does not include enhancements to JNI, although these may go into the JSR process.

JNI is the current standard programming interface for writing Java native methods and embedding the Java virtual machine into native applications. It governs the boundary between the JVM and the unmanaged native environment, providing protocols for data marshalling and object lifecycle management. According to JEP 191, these aspects of JNI are most painful to developers:

  • Requiring developers to write C code means they have to have expertise of a world completely different from Java.
  • Using JNI requires expertise usually not found in typical C and Java developers, since the developer must have at least some understanding of how the JVM manages memory and code.
  • Developers must be able to build that code for each platform they wish to support, or provide appropriate tooling for end-users to do the same.
  • The performance of JNI-based libraries is usually poor compared to the same library bound into a native application.
  • JNI acts as an opaque boundary for security. The JDK does not know what calls the functions in that library might use or whether the code in that library could compromise the stability or security of the JVM.

The difficulties of JNI can be addressed by a built-in FFI API for the JDK. The Java FFI would be easier to implement, and it would not require as much expertise in JVM internals and various host platforms. The FFI API will provide the following:

  • A metadata system to describe native library calls and native memory structure.
  • Mechanisms for discovering and loading native libraries.
  • Mechanisms for binding, based on metadata, a library/function or a memory structure to a Java endpoint.
  • Code for marshalling and unmarshalling Java data types to native data types.
JEP 191 can also add JIT optimization of native calls, GC-level awareness of native memory, and mechanisms to opt out of JNI safeguards known to be unnecessary. It will also include tooling for gathering function and memory metadata from native libraries, based on the ffi-gen library for JRuby. Security policies can also be set up that allow binding to specific functions, rather than the library-level permissions.

The need for a Java FFI has spawned the JNA and JNR libraries. The JNA library is more widely used. The JNR library is more comprehensive since it implements various levels of abstraction, provides function and memory metadata, abstracts away library and function binding. JNR has been in heavy use by the JRuby project and will likely form the basis of JEP 191.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread


Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you