In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to call CramCraft + local library in Java". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Write Java classes
Let's write a java class for Sample1
Public class Sample1 {public native int intMethod (int n); public native boolean booleanMethod (boolean bool); public native String stringMethod (String text); public native int intArrayMethod (int [] intArray); public static void main (String [] args) {System.loadLibrary ("Sample1"); Sample1 sample = new Sample1 (); int square = sample.intMethod (5); boolean bool = sample.booleanMethod (true) String text = sample.stringMethod ("Java"); int sum = sample.intArrayMethod (new int [] {1 booleanMethod: "+ square); System.out.println (" booleanMethod: "+ bool); System.out.println (" stringMethod: "+ text); System.out.println (" intArrayMethod: "+ sum) }}
There are four native methods above, which are four types of parameters, int, boolean, String, int [].
One of them is more important, which loads the dynamic class library
System.loadLibrary ("Sample1")
What loads under windows is Sample1.dll, and what loads under linux is Sample1.so.
Windowws is used in this article, so Sample1.dll is used later to represent the Sample1 dynamic link library.
Note: you cannot write the suffix dll or so. Also make sure that Sample1.dll is in the path path. This Sample1.dll is what we need to compile later.
The four native methods are the ones we need to implement in C.
Compile Sample1.java, using the command line (windows is cmd, linux is usually bash)
> javac Sample1.java
You can see the Sample1.class file
Generate header files using javah
Run on the command line
> javah Sample1
You can see a new file Sample1.h in the directory
/ * DO NOT EDIT THIS FILE-it is machine generated * / # include / * Header for class Sample1 * / # ifndef _ Included_Sample1 # define _ Included_Sample1 # ifdef _ cplusplus extern "C" {# endif / * * Class: Sample1 * Method: intMethod * Signature: (I) I * / JNIEXPORT jint JNICALL Java_Sample1_intMethod (JNIEnv *, jobject, jint) / * * Class: Sample1 * Method: booleanMethod * Signature: (Z) Z * / JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod (JNIEnv *, jobject, jboolean); / * * Class: Sample1 * Method: stringMethod * Signature: (Ljava/lang/String;) Ljava/lang/String; * / JNIEXPORT jstring JNICALL Java_Sample1_stringMethod (JNIEnv *, jobject, jstring) / * * Class: Sample1 * Method: intArrayMethod * Signature: ([I) I * / JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod (JNIEnv *, jobject, jintArray); # ifdef _ cplusplus} # endif # endif
We can see that there are four function declarations, Java_ full class name _ method name, complete class name includes package name, for example, demo.Sample1 is full class name, corresponding here is demo_Sample1.
In the comments we can see something like Signature, which is the signature of the method. With regard to Signature, here is a table to illustrate.
Java type Signature remarks booleanZbyteBcharCshortSintIlongLfloatFdoubleDvoidVobjectL uses / divides the complete class name for example: Ljava/lang/String represents the String type Array [signature for example: [I represents the int array, [Ljava/lang/String represents the String array Method (parameter signature) returns the type signature for example: ([I) I indicates that the parameter type is the int array, and returns the method of type int "
The * * function declaration of the header file above
JNIEXPORT jint JNICALL Java_Sample1_intMethod (JNIEnv *, jobject, jint)
The signature in the comment is Signature: (I) I
There are two parameters, JNIEnv * and jobject, in the argument list of each function, which will be explained later.
Implement the functions in the header file
You can use C language to achieve, you can also use C++ to achieve, let's talk about the implementation of C language.
# include "Sample1.h" # include JNIEXPORT jint JNICALL Java_Sample1_intMethod (JNIEnv * env, jobject obj, jint num) {return num * num;} JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod (JNIEnv * env, jobject obj, jboolean boolean) {return! boolean;} JNIEXPORT jstring JNICALL Java_Sample1_stringMethod (JNIEnv * env, jobject obj, jstring string) {const char* str = (* env)-> GetStringUTFChars (env, string, 0) Char cap; strcpy (cap, str); (* env)-> ReleaseStringUTFChars (env, string, 0); return (* env)-> NewStringUTF (env, strupr (cap));} JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod (JNIEnv * env, jobject obj, jintArray array) {int I, sum = 0; jsize len = (* env)-> GetArrayLength (env, array); jint * body = (* env)-> GetIntArrayElements (env, array, 0) For (I = 0; I)
< len; ++i) { sum += body[i]; } (*env)->ReleaseIntArrayElements (env, array, body, 0); return sum;}
(* env)-> GetStringUTFChars () this method is used to convert strings between Java and C, because Java itself uses double-byte characters, while C language itself is a single-byte character, so it needs to be converted.
JNIEnv * is a parameter for every function. It contains a lot of useful methods, such as Java reflection, and also provides such a transcoding function.
GetStringUTFChars () and NewStringUTF (), * convert the encoding format from UTF8 to C, and the second one returns a UTF8 string based on the string of C.
ReleaseStringUTFChars () is used to release objects. There are virtual machines for garbage collection in Java, but in C language, these objects must be collected manually. Otherwise, it may cause a memory leak.
The function can be guessed at a glance, as is the case with most function names in jni.h.
If it is C++, how should this code be written?
The following is the code of C++
# include "Sample1.h" # include JNIEXPORT jint JNICALL Java_Sample1_intMethod (JNIEnv * env, jobject obj, jint num) {return num * num;} JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod (JNIEnv * env, jobject obj, jboolean boolean) {return! boolean;} JNIEXPORT jstring JNICALL Java_Sample1_stringMethod (JNIEnv * env, jobject obj, jstring string) {const char* str = env- > GetStringUTFChars (string, 0); char cap [128] Strcpy (cap, str); env- > ReleaseStringUTFChars (string, 0); return env- > NewStringUTF (strupr (cap));} JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod (JNIEnv * env, jobject obj, jintArray array) {int I, sum = 0; jsize len = env- > GetArrayLength (array); jint * body = env- > GetIntArrayElements (array, 0); for (I = 0; I
< len; ++i) { sum += body[i]; } env->ReleaseIntArrayElements (array, body, 0); return sum;}
The above two ends of the code are very similar, except for one difference
C code: (* env)-> GetStringUTFChars (env, string, 0)
C++ Code: env- > GetStringUTFChars (string, 0)
C language is used in the structure of the function pointer, but in C++ or struct, we know that struct in C++ and class function is almost the same, struct can also be used to define classes, so env in C++ is a class object pointer.
Compile and run
The Microsoft compiler is used here to compile the C language version of dll.
> cl-I% Java home%\ include-I% Java home%\ include\ win32-LD Sample1.c-FeSample1.dll
Compile the C++ version of dll
> cl-I% Java home%\ include-I% Java home%\ include\ win32-LD Sample1.cpp-FeSample1.dll
Running
> java Sample1
Note: the 64-bit version of JDK may make an error when running the Times:
Java.lang.UnsatisfiedLinkError:... Sample1.dll: Can't load IA 32-bit .dll on an AMD 64-bit platform
If you have such an error, please use the 32-bit JDK to rerun it.
The running results are as follows:
This is the end of intMethod: 25 booleanMethod: false stringMethod: JAVA intArrayMethod: 36, "how to call Java + Local Library". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.