Java calls C language write so file
Because the work needs to write a so file, as a mobile phone hardware IC reading card and apk interaction Bridge, that is, middleware, read the internet has said that the implementation of JNI interface technology, here reproduced an example
1 //implemented with JNI2 //Example:3 4 //Create Helloworld.java5 classHelloWorld6 {7 Private native voidprint ();8 Public Static voidMain (string[] args)9 {Ten NewHelloWorld (). print (); One } A - Static - { theSystem.loadlibrary ("HelloWorld"); - } - } - //note the declaration of the Print method, the keyword native indicates that the method is implemented as a native code. Also note the system.loadlibrary Call of the static code snippet, which indicates that the Libhelloworld.so library is loaded automatically when the program loads. + //compiling Helloworld.java - //run the following command on the command line: + Javac Helloworld.java A //compiles the build Helloworld.class in the current folder. at //Generate HelloWorld.h - //run the following command on the command line: -Javah-JNI HelloWorld - //HelloWorld.h is generated in the current folder. Opening HelloWorld.h will find the following code: - /*Do not EDIT this file-it are machine generated*/ -#include <jni.h> in /*Header for Class HelloWorld*/ - to #ifndef _included_helloworld + #define _included_helloworld - #ifdef __cplusplus theextern "C" { * #endif $ /*Panax Notoginseng * Class:helloworld - * Method:print the * Signature: () V + */ AJniexportvoidJnicall Java_helloworld_print the(JNIENV *, jobject); + - #ifdef __cplusplus $ } $ #endif - #endif - //The file contains a declaration of the function java_helloworld_print. Here the bread contains two parameters, it is very important, the following is said when the implementation will be mentioned. the //Implement HELLOWORLD.C - //Create the Helloworld.c file by entering the following code:Wuyi#include <jni.h> the#include <stdio.h> -#include "HelloWorld.h" Wu -JniexportvoidJnicall AboutJava_helloworld_print (JNIENV *env, Jobject obj) $ { -printf ("Hello world!\n"); - } - //Note You must include the jni.h header file, which defines the various types, macro definitions, and so on that JNI uses. A //also need to pay attention to the two parameters of java_helloworld_print, this example is relatively simple, do not need to use these two parameters. However, these two parameters are very important in JNI. + //The env represents the Java Virtual machine environment, the Java passed the parameters and C is very different, you need to call the JVM provided by the interface to convert to C type, that is, by calling the Env method to complete the conversion. the //obj represents the called object, which is equivalent to this of C + +. When the C function needs to change the calling object member variable, it can be done by manipulating the object. - //Compile Build libhelloworld.so $ //under Linux, execute the following command to complete the compilation work: thecc-i/usr/lib/jvm/java-6-sun/include/linux/ the-i/usr/lib/jvm/java-6-sun/include/ the-fpic-shared-o libhelloworld.so helloworld.c the //generates libhelloworld.so in the current directory. Note that you must include the Include directory for Java (set according to your system environment) because JNI.H is included in the HELLOWORLD.C. - //Another notable thing is that in Helloworld.java we LoadLibrary methods to load "HelloWorld", but the library we generate is Libhelloworld. This is the Linux link rules, a library must be: lib+ Library name +.so. You only need to provide the library name when linking. in //Run the Java program HelloWorld the //at the end of the final step, it's time to validate the previous results: the Java HelloWorld About //If you have a problem with this step, if you receive an JAVA.LANG.UNSATISFIEDLINKERROR exception for this step, you can indicate the path to the shared library in the following way: theJava-djava.library.path= '. 'HelloWorld the //Of course there are other ways to indicate the path please refer to "Using JNI under Linux platform". the //we can see the long-lost "Hello world!" The output.
View Code
Try to finish, I generated a copy of the com_test_getmsg.h header file, and completed test.c, generate libtest.so file, Java calls so file, repeatedly reported:
Failed:cannot load Library:load_library (linker.cpp:761): Not a valid ELF executable:/data/app-lib/ Com.example.iccommtest-libtest.so
That is, the provided so cannot load and is valid.
Later, compared to the previous example, it is found that the problem is com_test_getmsg.h file name, because Com_test is the class name, and Getmsg is the method name, but the name of the method is the following:
1 /* 2 2 * Class: com_test33 * Method: getmsg44 * Signature: () V55 */6 6 jniexport jstring jnicall Java_ Com_test_getmsg7 7 (jnienv *, jobject);
View Code
Here is the problem, the method name ' _ ' is a special symbol, so Java in load writing so file, because COM is the class name, test is the method name, so the load failed. Well, it's a careless mess!
Regenerate the Test.h file with the method name:
1 /* 2 2 * Class: test33 * Method: getmsg44 * Signature: () V55 */6 6 jniexport jstring jnicall java_test_getmsg 7 7 (jnienv *, jobject);
View Code
This solves the problem!
I am non-professional Java development, write a bit bad, welcome to Java Cattle Spit Groove
Java calls C language write so file