Native keyword--java interface for calling non-Java code

Source: Internet
Author: User

Java Basics--jni Introduction (top)

Java? The native interface (Java Native interface,jni) is a standard Java API that enables the integration of Java code with code written in other programming languages . If you want to leverage existing code resources, you can use JNI as a key component in your toolkit-for example, in service-oriented architecture (SOA) and cloud-based systems. However, if you are not aware of something when you are using it, JNI can quickly lead to poor application performance and instability.

Development of JNI
JNI has been part of the Java platform since the JDK 1.1 release and has been extended in the JDK 1.2 release. The JDK 1.0 release contains an earlier native method interface, but does not explicitly separate native and Java code. In this interface, native code can go directly into the JVM structure, so it cannot be ported across JVM implementations, platforms, or even various JDK versions. Using the JDK 1.0 model to upgrade an application with a large number of native code and the overhead of developing native code that supports multiple JVM implementations is extremely expensive.
JNI support introduced in JDK 1.1:
Version Independence
Platform Independence
VM Independence
Developing a third-party class library
There is an interesting place to note that some of the younger languages (like PHP) are still struggling to overcome these problems in their native code support.
Integrate with legacy code to avoid rewriting.

The Java environment and language are very safe and efficient for application development. However, some applications need to perform some tasks that a pure Java program cannot accomplish, such as:

A. Inheritance with legacy code, avoiding rewriting

B. Implement features that are missing from the available class libraries. For example, when you implement ping in the Java language, you may need Internet Control Message Protocol (ICMP) functionality, but the base Class library does not provide it.
C. It is best to integrate with code written in C + + to fully exploit performance or other environment-related system features.

D. Address special cases where non-Java code is required.

E. Interacting with the operating system the JVM supports the Java language itself and the runtime library, which is the platform on which the Java program survives, consisting of an interpreter (the explanatory bytecode) and some libraries connected to the local code. However, after all, it is not a complete system, it often relies on some of the underlying (underneath) system support. These underlying systems are often powerful operating systems. By using the native method, we were able to implement the JRE's interaction with the underlying system in Java, and even some parts of the JVM were written in C, and if we were to use some of the Java language itself without providing the features of the encapsulated operating system, we would also need to use local methods.
F.sun ' s Java
Sun's interpreter is implemented in C, which allows it to interact with the outside like some normal C. Most of the JRE is implemented in Java, and it interacts with the outside world through some local methods. For example, the SetPriority () method of the class Java.lang.Thread is implemented in Java, but it implements the invocation of the Local Method SetPriority0 () in the class. This local method is implemented in C and is implanted inside the JVM, and on the Windows 95 platform, this local method will eventually invoke the Win32 setpriority () API. This is a specific implementation of a local method that is provided directly by the JVM, and more generally, the local method is provided by the external dynamic link library (external. dll) and then called by the JVM.

For example, the implementation of a core class library might require cross-package calls or bypass other Java security checks.
JNI allows you to complete these tasks. It explicitly separates the execution of Java code from native code (c + +) and defines a clear API for communicating between the two. To a large extent, it avoids direct memory references to the JVM from native code, ensuring that native code is written only once and can run across different JVM implementations or versions.
With JNI, native code can interact with Java objects at will, get and design field values, and invoke methods without being constrained by the same functionality as in Java code. This freedom is a double-edged sword: it sacrifices the security of Java code in exchange for the ability to accomplish the tasks listed above. Using JNI in your application provides a powerful, low-level access to machine resources (memory, I/O, and so on), so you won't be protected by a security net like normal Java developers. The flexibility and power of JNI leads to some programming risks, such as poor performance, bugs, and even program crashes. You must pay extra attention to the code in your application and use good practices to ensure the overall integrity of your application. Calls outside of Java are usually not ported to other platforms, and security exceptions can be thrown in applets. Implementing native code will make your Java application fail to pass 100% pure Java testing.

If you must perform a local call, there are several criteria to consider:
1. Encapsulate all your local methods in a single class that calls a single DLL. For each target operating system, you can replace the DLL with a version that is specific to the appropriate platform. This minimizes the impact of local code and helps to include the porting issues that you need later.
2. Local methods are simple. Try to minimize the dependency of your DLLs on any third-party (including Microsoft) run-time DLLs. Keep your local methods as independent as possible, minimizing the overhead required to load your DLLs and applications. If run-time DLLs are required, they must be supplied with the application.

The writing procedure of JNI
1. Writing a Java class with a native declaration method
2. Compiling Java classes using the Javac command
3. Use JAVA-JNI * * * * to generate a header file with a suffix of. h
4. Implementing local methods using other languages (c, C + +)
5. Generate a dynamic-link library of files written by local methods

The 10 most common coding and design errors encountered when using JNI, see tips and tools to avoid the 10 major JNI programming errors

Some tutorials can look at IBM's blog: Provides Java code examples that call local C code to implement Java native methods in Windows

The primary tutorial for JNI can look at the basics of Java--jni Introduction (top), Java Basics--jni Introduction (bottom)

Java Native Keywords

I. What is native Method
In a nutshell, a native method is a Java interface that calls non-Java code. A native method is a Java approach: the implementation of this method is implemented by non-Java languages, such as C. This feature is not specific to Java, many other programming languages have this mechanism, such as in C + +, you can use the extern "C" to tell the C + + compiler to call a C function.
"A native method is a Java method whose implementation was provided by Non-java code."
When defining a native method, the implementation body is not provided (some like the definition of a Java interface) because its implementation is implemented outside the non-Java language.

The identifier native can be used with all other Java identifiers, except for abstract. This is justified because native implies that these methods are implemented, except that the implementations are non-Java, but the abstract clearly indicates that these methods are not implemented. Native and other Java identity Connect prompt time, its meaning and non-native method is not different.

A native method can return any Java type, including non-basic types, and can also be used for exception control. The implementation of these methods can make an exception and throw it, which is very similar to the Java approach.
The presence of the native method does not have any effect on other classes calling these local methods, and the other classes that actually call these methods do not even know that it is calling a local method. The JVM controls all the details of calling the local method.

Native Introduction to the native keyword extracted from Java

Since this native keyword is temporarily unused, time is limited and is now recorded only here. Perfect again later.

Native keyword--java calling non-Java code interfaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.