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 solve the big problem of unable to write Java by writing locally in JNI

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

Share

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

This article shows you how to write locally through JNI to solve the big problem of Java can not be written, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Using JNI in your program is not an easy task, however, the performance of JNI and the ability to use the original code will add more functionality to your Java program and meet more challenges.

The Java native interface (JNI--Java Native Interface) allows code running on the Java Virtual Machine (JVM--Java Virtual Machine) to call local programs and class libraries, which can be written in other languages, such as C, C++, or assembly language. When a program cannot be written entirely in Java, developers can write native methods through JNI, such as platform-dependent features or libraries that are not supported by the standard Java class library. JNI can also be used to modify existing programs written in other languages so that they can be accessed through programs written by Java. Many basic class libraries rely on JNI to provide services to developers and users, such as file input / output and audio functions. The performance-and platform-sensitive API included in the basic class library allows all Java programs to use these features in a secure and platform-independent manner. Before adopting JNI, developers need to be clear that these features are not already included in the Java standard class library. In this article, I will explain how JNI works and how native types map to Java types and class libraries.

How JNI works

In JNI, local functions are implemented through a separate .c or .cpp file (C++ provides a more concise interface for JNI). When JVM calls the function, it passes a JNIEnv pointer, a jobject pointer, and the Java parameter defined by the Java method, as shown in the form of the JNI function:

JNIEXPORT void JNICALL Java_ClassName_MethodName (JNIEnv * env, jobjectobj) {/ / Method native implemenation}

The env pointer is a structure that contains the JVM interface, which contains the functions necessary to interact with JVM and work with Java objects. The JNI function in the example can convert between local array and Java array types, and between local string and Java string types. Its functions also include object instantiation, throwing exceptions, and so on. Basically you can use JNIEnv to do everything Java can do, though it's much simpler. A more formal explanation is that native code accesses JVM by calling JNI's function, which is achieved through an interface pointer (the interface pointer is actually a pointer to a pointer), which points to an array of pointers, each pointer to an interface function, and each interface function is predefined in the array. Native methods take the JNI interface pointer as a parameter, and if multiple calls to the local method occur in the same Java thread, JVM guarantees that the same interface pointer is passed to the local method. However, a local method can be called by different JNI threads, so it may also receive different Java interface pointers.

The local method is loaded through the System.loadLibrary method. In the following example, the initialization method of the class loads a local class library for the specified platform, which defines the local method:

Packagepkg; class Cls {native double f (inti, String s); static {System.loadLibrary (pkg_Cls ");}}

The parameter of the System.loadLibrary method is the name of a class library, which can be chosen by the programmer, and the system follows a standard localization platform to convert the name of the class library to the name of a local class library. For example, in the Solaris operating system, pkg_Cls is converted to libpkg_Cls.so, while the Win32 system converts the same pkg_Cls to pkg_Cls.dll.

Dynamic pointers resolve by their names, and the names of a local method are concatenated by component, which contains the prefix "Java_", a detached legal class name, and a detached method name. Note: Microsoft JVM has the same mechanism for calling native Windows code from Java, which is called the original native interface (Raw Native Interface (RNI)). Basic types, such as integers, characters, and so on, are copied between Java and native code, while other custom Java objects are passed by reference. This table shows the type mapping between Java and native code, these types are interchangeable, you can use jint types where you use int types, and vice versa, and no type conversions are required. However, the conversion between Java string and array types and local string and array types is more difficult. If the character "*" appears in the jstring type you use, your code will cause JVM to crash. The following example shows how you should use strings correctly:

JNIEXPORT void JNICALL Java_ClassName_MethodName (JNIEnv * env, jobjectobj, jstringjavaString) {/ / Get the native string from Java string const char * nativeString = env- > GetStringUTFChars (env,javaString, 0); printf ("% s", nativeString); env- > ReleaseStringUTFChars (env,javaString, nativeString);}

You need to use the interface pointer env to manipulate the Java object.

The above is how to write locally in JNI to solve the big problem that Java can't be written. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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