In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces you how to use JNI, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Java Native Interface (JNI) is a mixed programming method that uses the Java language and the native Cmax programming language. It allows Java code running in the Java Virtual Machine (VM) to interoperate with applications and libraries written in other programming languages (such as C, C++ and assembly). It supports loading code from dynamic link libraries and can use the efficient features of Cinning +.
If you want a basic understanding of the function and use of JNI, you can read the Java Native Interface document https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/jniTOC.html
At the same time, this article will introduce some usage details that may not be noticed at first.
Problem with FindClass not finding the class
The JNI class name starts with the package name and is separated by'/', for example
"java/lang/String"
If you are looking for an array class, the class name should be in the form of a signature, so the class of an one-dimensional string array is defined as
"[Ljava/lang/String;"
You can refer to https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html#wp9502
But at present, even if all the signed class names are passed in, FindClass will still find the corresponding class. The reason is a problem left over by history, please refer to https://bugs.openjdk.java.net/browse/JDK-6411605
To ensure correctness, you can verify that the class name construction is standardized by adding the parameter-Xcheck:jni when running the class file.
The problem of incorrect function name generated by javah
When the function is overloaded and the inner class type is used as the parameter of an overloaded function, the problem of incorrect function name generated by javah will be triggered. Javah will generate the JNI function name according to JNI's name resolution rules, but JVM will not find the function according to this name at runtime, so the function will not be found at runtime.
Reference https://bugs.openjdk.java.net/browse/JDK-8145897
Package p
Class A {
Static class B {}
Static class C {
Native static long Foo (B bar)
}
}
Run javah-jni-o jni_libA.h-classpath. / p.A
The expected output is:
/ / Method: Foo
/ / Signature: (Lp/A$B;) J
JNIEXPORT jlong JNICALLJava_p_A_00024C_Foo__Lp_A_00024B_2
But the actual output is:
/ / Method: Foo
/ / Signature: (Lp/A/B;) J
JNIEXPORT jlong JNICALLJava_p_A_00024C_Foo__Lp_A_B_2
The'$'of inner class B is ignored.
There are two alternative solutions:
1. Overloaded functions with inner classes as parameters are not written to local methods
two。 Manually add'$'('00024') after generating the method
Exception Mechanism in JNI
Most JNI methods cannot be called when there are exceptions waiting to be handled. To expect and return an exception through the return value of the function ExceptionCheck or ExceptionOccurred, or handle and clear the exception.
If you want to get a description of the exception, you need to find the Throwable class, call its getMessage "() Ljava/lang/String;" method, and use GetStringUTFChars to get the content.
Many JNI methods will throw exceptions, but some exceptions will not be thrown. You need to add conditional judgment where the exception should be thrown, and use ThrowNew to construct exceptions, the most common of which are java.lang.ArithmeticException and Java.lang.NullPointerException.
The value and assignment of primitive array in JNI
GetArrayElements and GetStringChars are very useful ways to write or read an array.
The GetArrayElements series functions allow you to return a pointer to the actual element, or to allocate some memory to copy the data. The pointer to the returned raw data is guaranteed to be valid until the release method is called, and each fetched array must be released manually. At the same time, you can decide whether to copy the data by passing a non-null pointer as a parameter to the isCopy.
Jbyte* data = env- > GetByteArrayElements (array, NULL)
If (data! = NULL) {
Memcpy (buffer, data, len)
Env- > ReleaseByteArrayElements (array, data, JNI_ABORT)
}
The above code is a simple example that takes the array, then copies the len-length byte, and finally releases the array.
There is an easier way to do the same thing
Env- > GetByteArrayRegion (array, 0, len, buffer)
This approach has several benefits:
1. Only one JNI call is needed to reduce overhead.
two。 There is no need to restrict the original data or make additional copies of the data
3. Reduce the risk of forgetting to release after some mistakes
4. Will throw an exception when it crosses the line
Similarly, the SetArrayRegion series method assigns values to the elements in the array, and GetStringRegion or GetStringUTFRegion gets the characters in the String.
The application of the pay shield SDK protection system inputs the aar package. By analyzing the aar structure, the class file to be protected is transformed into AST (Abstract Syntax Tree), and the AST is homomorphic translated into the corresponding local code. The localized transformation rules correspond to the number of AST nodes one by one. Because bytecode is managed code and runs in VM (java code runs on JVM and Android bytecode runs on Dalvik), native code cannot express the corresponding semantics for manipulating the VM memory model and behaviors involving VM features, such as creating objects and throwing exceptions. For AST nodes with such semantics, the translation of local code follows the JNI invocation specification, which ensures the correctness of localization translation while taking into account compatibility.
On how to use JNI to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.