JNI is a powerful feature of the Java platform. Applications can use JNI to integrate C/C ++ code into Java programs. With JNI, developers do not have to give up investment in the original code while leveraging the powerful functions of the Java platform. Because JNI is a standard interface defined by the Java platform, when a programmer moves to the Java code set cost library, as long as the language interoperability problem is solved on one platform, the solution can be easily transplanted to other Java platforms.
For example, if you have added a local library to Dalvik, you can also easily port this local library to j2se and Apache harmony, Because Java and C/C ++ are interoperable, everyone follows a set of API interfaces, namely JNI.
Composition of Java platform: Java Vm and Java API. Java applications are developed in Java and compiled into bytecode (. Class file) unrelated to the platform ). Java APIs consist of a set of predefined classes. The Java platform implemented by any organization must support: Java programming language, virtual machine, and API (TRANSLATOR: Sun has clear specifications for Java language, virtual machine, and API ).
Platform environment: Operating System, a group of libraries, and CPU instruction sets. Local applications are usually dependent on a specific platform environment, developed in C, C ++, and other languages, and compiled into platform-related binary commands, the target binary code is generally not portable among different operating systems. Java platform (Java Vm and Java API) is generally developed on a platform. For example, sun's Java Runtime Environment (JRE) supports all the efforts made on Unix-like and windows. Java platforms to make programs more portable.
When the Java platform is deployed to a local system, it is necessary to make Java programs work together with local code. Partly because of the legacy code (protecting the original investment) Problem (some efficiency-sensitive code is implemented in C, but now JavaVM's execution efficiency is completely reliable ), engineers started to build Java applications based on C/C ++ for a long time. Therefore, C/C ++ code will coexist with Java applications for a long time.
JNI allows you to write programs in other languages while leveraging the powerful Java platform. As part of JavaVM, JNI is a set of Bidirectional interfaces that allow Java to interoperate with local code. As a bidirectional interface, JNI supports two types of local code: local library and local application. Use local code to implement the native method interface defined in Java, so that Java calls local code. Through JNI, You can embed Java VM into an application. At this time, the Java platform is used as an enhancement of the application, so that it can call the Java class library, for example, to run the applet in the browser. When the browser encounters the "applet" tag, the browser will hand over the content in the tag to the Java Vm for explanation and execution, it is typical to embed JavaVM into the browser. JNI is not just a set of interfaces, but also a set of use rules. The Java language has the "native" keyword and specifies which methods are implemented using local code. during translation, the "Native method" is processed differently based on the context meaning. When the native method refers to the Methods Modified with the "native" keyword in Java, It is not translated; when the code is implemented in C/C ++, it is translated into local code.
As mentioned above, the method for embedding a Java VM in an application is the best choice for the most powerful extension of the application. At this time, your application can freely use all the functions of the Java API; everyone is interested in reading how the browser extends the applet or reading something from the palm WebOS.
Our entire team made two platforms for expansion. It took nearly a year and a half to design, code, test, and debug, and the code volume was around 14000 lines. After the expansion, the platform functions were unprecedentedly enhanced. I feel that it is rare to develop software without coding. It is difficult to design, test, debug, and optimize software at the beginning and to make it commercially available. This requires the final product to be a powerful and stable platform, achieving this goal is a long term. how many years have Java, windows, Linux, QT, and WebKit been developed? Salute to all software engineers!
Remember that when a Java program integrates local code, it will lose some java benefits.
First, you must solve the portability problem by yourself after leaving Java, and re-compile the local library on other platforms.
Second, you must be careful with all aspects of JNI programming and the details of the C/C ++ language itself. Improper handling will cause the application to crash.
General principle: Do a Good Job of application architecture so that native methods can be defined in as few classes as possible.
Learning JNI programming is a long practical process and involves numerous problems.
Use C/C ++ for programming. Common problems include memory leakage and pointer out-of-bounds. In addition, JNI is used, and JavaVM problems must be solved:
? After a new Java object is added to the local code, it is expected to maintain the reference of this object in the local code. How can this problem be avoided by GC;
? Java's object-oriented language encapsulation is damaged. Any methods and attributes in the Java class are visible to JNI, whether it is public or private/protected/package.
? Poor management of localref/globalref may cause table overflow exception and application crash
? The process of calling Java from JNI is not very intuitive. It usually takes hundreds of lines to implement it with JNI, which can be done by several lines of Java code.
Before using JNI in a project, consider whether there are other more suitable solutions. In the previous section, you should pay enough attention to the shortcomings of JNI. Here we will introduce several technologies that do not interact with other languages through JNI:
IPC or the TCP/IP network solution (Android ase)
For databases, you can use JDBC
Java distributed object technology: Java idl api ipc and TCP/IP are common protocol-based information exchange solutions. For details, see binder and ASE (Android script environment) on Android ).
A typical solution is to run Java programs and local code in different processes separately. The biggest advantage of adopting process splitting is that the crash of one process does not affect another process immediately. However, it is sometimes necessary to place Java code and local code in a process. As follows:
Java APIs may not support functions related to some platforms. For example, you need to use file types not supported by Java APIs during application execution. If you use cross-process operations, it is cumbersome and inefficient to avoid inefficient data copy operations between processes multi-process derivation: time-consuming, resource-consuming (memory) using local code or assembly code to override inefficient methods in Java
In short, if Java must interact with the local code that resides in the same process, use JNI. Writing code is a skill and art. It depends on how much effort you want to design. For example, Chrome is a model of multi-process. It is concise, efficient, and amazing. The question about how Java applications interact with local code was raised in the early days of the Java platform. jdk1.0 included a set of interfaces for interacting with local code. At that time, many Java methods and libraries were dependent on local methods (such as Java. Io and java.net ). However, JDK Release 1.0 has two main problems: the Java Virtual Machine specification does not define the object layout, members of the local code access object can obtain the address of the object in the memory by accessing the Local Code implemented by members of the C structure. Therefore, the local method is related to GC. To solve the above problem, JNI is re-designed to make this interface Easy to support on all platforms. Virtual Machine implementers use JNI to support a large number of local code tools. Developers do not have to deal with different types of local interfaces. All JNI developers face the JavaVM operation specification. apijni's first support is in JDK release 1.1, however, the interaction between Java and local code in 1.1 still uses the original method (JDK 1.0 ). however, this situation does not last for a long time. In Java 2 SDK release1.2, the interaction between the Java layer and local code is rewritten using JNI.
As part of the JavaVM specification, the interaction between the Java layer and local code should be implemented through JNI.
JNI Summary (1)