Java Native Interface
Encyclopedia
|
| Tutorials | Encyclopedia | Dictionary | Directory |
|
Java Native Interface
The Java Native Interface (JNI) is a programming framework that allows Java code running in the Java virtual machine (JVM) to call and be called[1] by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages, such as C, C++ and assembly.
Purpose and featuresThe JNI is used to write native methods to handle situations when an application cannot be written entirely in the Java programming language such as when the standard Java class library 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 the 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 the 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 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 valve" for Java developers because it allows them to add functionality to their Java Application that the Java API can't provide. It can be used to interface with code written in other languages, like 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. PitfallsThe JNI is not trivial and requires a considerable effort to learn, and some people recommend that only advanced programmers should use the JNI. However, the capability for Java to communicate with C++ and assembly removes any limitations on what function Java programs can perform. Programmers considering using the JNI should be aware that
Java automated garbage collection is quite different from C malloc/free because it can move objects after they are allocated. It is thus vital that pointers to Java objects are obtained and locked correctly. C programmers often do not understand this which can lead to very obscure and unreproduceable errors. JNI needs great caution and is generally avoided by Java developers. For example, most Java JDBC databases communicate directly with a socket rather than use existing C side APIs. How the JNI worksIn JNI, native functions are implemented in a separate .c or .cpp files. (C++ provides a slightly cleaner interface with JNI.) When the JVM invokes the function, it passes a The For example, the following converts a Java string to a native string: 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 Native data types can be mapped to/from Java data types. For compound types such as objects, arrays and strings the native code must explicitly convert the data by calling methods in the Mapping typesThe following table shows the mapping of types between Java and native code.
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 Here, these types are interchangeable. You can use However, mapping between Java Strings and arrays to native strings and arrays is different. If you use a This is similar with Java arrays, as illustrated in the example below that takes the sum of all the elements in an array. 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: JNIEnv *env; (*g_vm)->AttachCurrentThread (g_vm, (void **) &env, NULL); // do stuff (*g_vm)->DetachCurrentThread (g_vm); Advanced usesNative AWT paintingNot only can native code interface with Java, it can also draw on a Java , which is possible with the Java AWT Native Interface. The process is almost the same, with just a few changes. The Java AWT Native Interface is only available since J2SE 1.3. Access to assembly codeJNI also allows direct access to assembly code, without even going through a C bridge[2]. Accessing Java applications from assembly is also possible in the same way[3]. Microsoft's RNIMicrosoft's proprietary implementation of a Java Virtual Machine (Visual J++) had a similar mechanism for calling native Windows code from Java, called the Raw Native Interface (RNI). However, following the Sun - Microsoft litigation about this implementation, Visual J++ is no longer maintained. See also
ReferencesExternal links
de:Java Native Interface es:Java Native Interface fr:Java Native Interface it:Java Native Interface nl:Java Native Interface ja:Java Native Interface pl:Java Native Interface pt:JNI ru:Java Native Interface Source: Wikipedia | The above article is available under the GNU FDL. | Edit this article
|
|
top
©2008-2009 TutorGig.com. All Rights Reserved. Privacy Statement