A software framework, in computer programming, is an abstraction in which common code providing generic functionality can be selectively overridden or specialized by user code providing specific functionality.... that allows Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java .... code running in a Java Virtual Machine
Java Virtual Machine
A Java Virtual Machine is a set of computer software programs and data structures which use a virtual machine model for the execution of other computer programs and Scripting language.... (JVM) to call and to be called by native applications (programs specific to a hardware
Hardware
Hardware is a general term that refers to the physical cultural artifacts of a technology. It may also mean the physical components of a computer system, in the form of computer hardware.... and operating system
Operating system
An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer.... platform) and libraries written in other languages, such as C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... , C++
C++
C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features.... and assembly
Assembly language
An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture.... .
Purpose and features JNI allows one to write native methods to handle situations when an application cannot be written entirely in the Java programming language e.g.
Discussion
Ask a question about 'Java Native Interface'
Start a new discussion about 'Java Native Interface'
A software framework, in computer programming, is an abstraction in which common code providing generic functionality can be selectively overridden or specialized by user code providing specific functionality.... that allows Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java .... code running in a Java Virtual Machine
Java Virtual Machine
A Java Virtual Machine is a set of computer software programs and data structures which use a virtual machine model for the execution of other computer programs and Scripting language.... (JVM) to call and to be called by native applications (programs specific to a hardware
Hardware
Hardware is a general term that refers to the physical cultural artifacts of a technology. It may also mean the physical components of a computer system, in the form of computer hardware.... and operating system
Operating system
An operating system is an interface between hardware and applications; it is responsible for the management and coordination of activities and the sharing of the limited resources of the computer.... platform) and libraries written in other languages, such as C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... , C++
C++
C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features.... and assembly
Assembly language
An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture.... .
Purpose and features
JNI allows one to write native methods to handle situations when an application cannot be written entirely in the Java programming language e.g. when the standard Java class
Class (computer science)
In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share.... library
Library (computer science)
In computer science, a library is a collection of subroutines or Class used to develop software. Libraries contain code and data that provide services to independent programs.... does not support the platform-specific features or program library. It is also used to modify an existing application, written in another programming language, to be accessible to Java applications. Many of the standard library classes depend on JNI to provide functionality to the developer and the user, e.g. I/O file reading and sound capabilities. Including performance- and platform-sensitive API implementations in the standard library allows all Java applications to access this functionality in a safe and platform-independent manner. Before resorting to using JNI, developers should make sure the functionality is not already provided in the standard libraries.
The JNI framework lets a native method utilize Java objects
Object (computer science)
In its simplest embodiment, an object is an allocated region of storage. Since programming languages use variable#Computer_programmings to access objects, the terms object and variable are often used interchangeably.... in the same way that Java code uses these objects. A native method can create Java objects and then inspect and use these objects to perform its tasks. A native method can also inspect and use objects created by Java application code.
JNI is sometimes referred to as the "escape hatch" for Java developers because it allows them to add functionality to their Java application that the standard Java APIs cannot otherwise provide. It can be used to interface with code written in other languages, such as C and C++. It is also used for time-critical calculations or operations like solving complicated mathematical equations, since native code can be faster than JVM code.
Pitfalls
subtle errors in the use of JNI can destabilize the entire JVM in ways that are very difficult to reproduce and debug;
only applications and signed applets can invoke JNI;
an application that relies on JNI loses the platform portability Java offers (a workaround is to write a separate implementation of JNI code for each platform and have Java detect the operating system and load the correct one at runtime);
the JNI framework does not provide any automatic garbage collection for non-JVM memory resources allocated by code executing on the native side. Consequently, native side code (such as C, C++, or assembly language) must assume the responsibility for explicitly releasing any such memory resources that it itself acquires;
error checking is a MUST or it has the potential to crash the JNI side and the JVM.
on Linux and Solaris platforms, if the native code registers itself as a signal handler it could intercept signals intended for the JVM. Signal chaining should be used to allow native code to better interoperate with JVM.
on Windows platforms Structured Exception Handling (SEH) may be employed to wrap native code in SEH try/catch blocks so as to capture machine (CPU/FPU) generated software interrupts (such as NULL pointer access violations and divide-by-zero operations) and to handle these situations before the interrupt is propagated back up into the JVM (i.e. Java side code) and in all liklihood resulting in an unhandled exception;
How the JNI works
In the JNI framework, native functions are implemented in separate .c or .cpp files. (C++ provides a slightly cleaner interface with JNI.) When the JVM invokes the function, it passes a JNIEnv pointer, a jobject pointer, and any Java arguments declared by the Java method. A JNI function may look like this:
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj)
The env pointer is a structure that contains the interface to the JVM. It includes all of the functions necessary to interact with the JVM and to work with Java objects. Example JNI functions are converting native arrays to/from Java arrays, converting native strings to/from Java strings, instantiating objects, throwing exceptions, etc. Basically, anything that Java code can do can be done using JNIEnv, albeit with considerably less ease.
For example, the following converts a Java string to a native string:
//C++ code
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj, jstring javaString)
Note that C++ JNI code is syntactically slightly cleaner than C JNI code because like Java, C++ uses object method invocation semantics. That means that in C, the env parameter is dereferenced using (*env)-> and env has to be explicitly passed to JNIEnv methods. In C++, the env parameter is dereferenced using env-> and the env parameter is implicitly passed as part of the object method invocation semantics.
A data type in programming languages is an attribute of a data which tells the computer something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it.... s can be mapped to/from Java data types. For compound types such as objects, array
Array
In computer science, an array is a data structure consisting of a group of element s that are accessed by index . In most programming languages each element has the same data type and the array occupies a contiguous area of computer memory.... s and string
String (computer science)
In computer programming and some branches of mathematics, a string is an ordered sequence of symbols. These symbols are chosen from a predetermined set or alphabet.... s the native code must explicitly convert the data by calling methods in the JNIEnv.
Mapping types
The following table shows the mapping of types between Java and native code.
Native Type
Java Language Type
Description
Type signature
unsigned char
jboolean
unsigned 8 bits
Z
signed char
jbyte
signed 8 bits
B
unsigned short
jchar
unsigned 16 bits
C
short
jshort
signed 16 bits
S
int
jint
signed 32 bits
I
long long __int64
jlong
signed 64 bits
J
float
jfloat
32 bits
F
double
jdouble
64 bits
D
In addition, the signature "L fully-qualified-class ;" would mean the class uniquely specified by that name; e.g., the signature "Ljava/lang/String;" refers to the class java.lang.String. Also, prefixing [ to the signature makes the array of that type; for example, [I means the int array type.
Here, these types are interchangeable. You can use jint where you normally use an int, and vice-versa, without any typecasting required.
However, mapping between Java Strings and arrays to native strings and arrays is different. If you use a jstring in where a char * would be, your code could crash the JVM.
Of course, there is much more to it than this. Look for links below for more information.
JNIEnv*
In each native call JNIEnv argument is only valid during the call. To use the argument outside the call you need to use AttachCurrentThread and DetachCurrentThread, like so:
Not only can native code interface with Java, it can also draw on a Java , which is possible with the Java AWT Native Interface
Java AWT Native Interface
Java AWT Native Interface is an interface for the Java that enables rendering library compiled to native code to draw directly to a Java Abstract Window Toolkit object drawing surface.... . The process is almost the same, with just a few changes. The Java AWT Native Interface is only available since J2SE
Java Platform, Standard Edition
Java Platform, Standard Edition or Java SE is a widely used Platform for programming in the Java language. It is the Java Platform used to deploy porting Application software for general use.... 1.3.
An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture.... , without even going through a C
C (programming language)
C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system.... bridge. Accessing Java applications from assembly is also possible in the same way.
Microsoft's RNI
Microsoft's proprietary implementation of a Java Virtual Machine (Visual J++
Visual J++
Visual J++ was Microsoft's specific implementation of Java . Optimized for the Microsoft Windows, J++ programs could only run on the MSJVM , which was Microsoft's attempt at a faster Interpreter .... ) had a similar mechanism for calling native Windows code from Java, called the Raw Native Interface (RNI). However, following the Sun - Microsoft litigation
Visual J++
Visual J++ was Microsoft's specific implementation of Java . Optimized for the Microsoft Windows, J++ programs could only run on the MSJVM , which was Microsoft's attempt at a faster Interpreter .... about this implementation, Visual J++
Visual J++
Visual J++ was Microsoft's specific implementation of Java . Optimized for the Microsoft Windows, J++ programs could only run on the MSJVM , which was Microsoft's attempt at a faster Interpreter .... is no longer maintained.
Java AWT Native Interface is an interface for the Java that enables rendering library compiled to native code to draw directly to a Java Abstract Window Toolkit object drawing surface....
GlueGen is a Java tool which automatically generates the Java and Java Native Interface code necessary to call C programming language libraries from Java code.... A Java tool which automatically generates the Java and JNI code necessary to call C libraries from Java code.
Platform Invocation Services, commonly referred to as P/Invoke, is a feature of Common Language Infrastructure implementations, like Microsoft's Common Language Runtime, that enables managed code to call native code.... , the .NET Framework
.NET Framework
The Microsoft .NET Framework is a software framework that is available with several Microsoft Windows operating systems. It includes a large Library of coded solutions to prevent common programming problems and a virtual machine that manages the execution of programs written specifically for the Software framework.... equivalent of JNI.
SWIG is an open source software tool used to connect computer program or library written in C /C++ with scripting languages such as Tcl, Perl, Python , Ruby programming language, PHP, Lua , R_language and other languages like Java , C Sharp programming language, Scheme and Ocaml.... is a multilanguage interface generator for C and C++ libraries that can generate JNI code
Java Native Access provides Java programs easy access to Shared library without using the Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort.... provides Java programs easy access to native shared libraries without writing boiler plate code
Boilerplate (text)
Boilerplate is any text that is or can be reused in new contexts or applications without being changed much from the original. Many computer programmers often use the term #Boilerplate code....
External links
provides simplified access to native code from Java applications without using Java Native Interface.
LGPL library to call native functions from Java
Access to native libraries from Java without JNI
Another library for access to native libraries without JNI
Library to access native code without JNI
Annotation based pure Java access to Win32 API or Linux/Mac OS X shared libraries, similar to P/Invoke
Transparent Native Programming for C/C++, pure Java alternative to JNI using POJOS and JDO/JPA development style