Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Java keyword native

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use the Java keyword native". In the daily operation, I believe many people have doubts about how to use the Java keyword native. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the Java keyword native". Next, please follow the editor to study!

The native keyword is found in many classes in the JDK source code, and in the Object.java class, the getClass () method, hashCode () method, clone () method, and so on, are modified with the native keyword.

Public final native Class getClass (); public native int hashCode (); protected native Object clone () throws CloneNotSupportedException

So why use native to decorate the method, and what's the use of that?

1 、 JNI:Java Native Interface

Before introducing native, let's take a look at what JNI is.

In general, we can write programs in the Java language, but in some cases, Java may not meet the needs of the application, or may not better meet the needs, such as:

① and standard Java class libraries do not support the platform-related functions required by the application platform.

②, we have written a class library in another language, how to call it in Java code?

③, some method code that runs a lot of times, in order to speed up performance, we need to write it in a language that is closer to hardware, such as assembly.

In the final analysis, the above three requirements are how to use Java code to call code written in different languages. So JNI came into being.

The Java Native Interface (JNI) standard has been part of the java platform since Java 1.1, allowing Java code to interact with code written in other languages. JNI was originally designed for native compiled languages, especially C and C++, but it doesn't prevent you from using other languages, as long as the calling convention is supported. Using java to interact with locally compiled code usually loses platform portability. However, this is acceptable or even necessary in some cases, such as using old libraries to interact with hardware, operating systems, or to improve program performance. The JNI standard at least ensures that native code works under any Java virtual machine implementation.

Through JNI, we can call the library functions of the operating system related technologies through Java programs (code), so as to interact with other technologies and systems, and use other technologies to realize the functions of the system; at the same time, other technologies and systems can also call the functions implemented inside the Java application system through the corresponding native interfaces provided by JNI.

On windows systems, generally executable applications are based on the PE structure of native, and JVM on windows is also based on native structure. Java application systems are built on top of JVM.

One might ask, isn't Java cross-platform? If you use JNI, won't the program lose the advantage of cross-platform? That's true.

Disadvantages of JNI:

①, programs no longer cross-platform. To cross-platform, the local language part must be recompiled in a different system environment.

②, programs are no longer absolutely safe, and improper use of native code may cause the entire program to crash. A general rule is that you should centralize local methods in a few classes. This reduces the coupling between JAVA and C.

At present, the disadvantages of using JNI relative to the advantages are acceptable, maybe with the development of Java technology, we no longer need JNI, but at present JDK still provides support for JNI standards.

2. The local method of writing programs in C language

The above explains what JNI is, so let's write an example of how to call a local C program with Java code.

The official documents are as follows: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/jniTOC.html

The steps are as follows:

①, write the java class of the method with the native declaration, and generate the .java file; (note that the method keyword declared by native appears here)

②, use the javac command to compile the java class and generate the .class file.

③, using the javah-jni java class name to generate a header file with the extension h, that is, a .h file

④, implement native methods using CumberCraft + (or other programming languages), and create the implementation of .h files, that is, create .cpp files to implement the methods in .h files.

⑤, generate dynamic link library (DLL) from files written by Candlespace +, and generate dll files

Let's complete these steps through a call to a HelloWorld program.

Note: all of the following operations are done in the directory: d:\ JNI.

The java class public class HelloJNI {/ / native keyword that writes a method with a native declaration tells JVM that the method is called to define private native void helloJNI () externally; static {System.loadLibrary ("helloJNI"); / / load the local library} public static void main (String [] args) {HelloJNI jni = new HelloJNI (); jni.helloJNI ();}}

Notify the JVM call with the method declared by native, which is defined externally, that is, we will implement it in C language.

System.loadLibrary ("helloJNI"); loads the dynamic library, and the parameter helloJNI is the name of the dynamic library. We can understand it this way: the method helloJNI () in the program is not implemented in the program, but what should we do if we call this method next? We need to initialize this method, so initialize it with a static code block.

At this point, if we run the program directly, we will report a "A Java Exception has occurred" error:

Second, use the javac command to compile the java class and generate the .class file

After executing the above command, generate the HelloJNI.class file:

Third, use the javah-jni java class name to generate a header file with the extension h

After executing the above command, there is an extra HelloJNI.h file in the D:/JNI directory:

Fourth, use C language to implement local methods.

If you don't want to install visual studio, we need to install gcc on the windows platform.

Pay attention to the choice of installation version, depending on whether the system is 32-bit or 64-bit. 64-bit click to download.

After the installation is completed, pay attention to configure the environment variable, and enter Gateway +-v in cmd. If the following information appears, the installation and configuration is complete:

Then enter the following command:

Gcc-M64-Wl,--add-stdcall-alias-I "C:\ Program Files\ Java\ jdk1.8.0_152\ include"-I "C:\ Program Files\ Java\ jdk1.8.0_152\ include\ include\ win32"-shared-o helloJNI.dll helloJNI.c

-M64 means that the generated dll library is 64-bit. The following path represents the JDK path installed on this machine. An additional helloJNI.dll file has been added after generation

Finally, run HelloJNI: output HelloJNI! The great task has been completed.

3. The flow chart of JNI calling C

4. Native keyword

Through the introduction of so much knowledge of JNI above, it is finally time to introduce the protagonist of this article-the native keyword. I believe that after reading the above introduction, you should also know what native is.

Native is used to modify the method, notifying the JVM call with the method declared by native, which is defined externally and can be implemented in any language. In a nutshell, a native Method is an interface for Java to call non-Java code.

Native syntax:

The location of ① and decorated methods must be before the return type, and there is no restriction on the relationship between them and the rest of the method controllers.

②, cannot be decorated with abstract, there is no method body, and there are no left and right braces.

③, return value can be any type

At this point, the study of "how to use the Java keyword native" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report