JAVA calls the SO file written in C language, and java calls cso

Source: Internet
Author: User

JAVA calls the SO file written in C language, and java calls cso

JAVA calls the SO file written in C Language

Because the job needs to write a SO file, which serves as a bridge between the mobile phone hardware IC card reading and APK interaction, that is, middleware. After reading the JNI interface technology implementation on the internet, I have reproduced an example here.

1 // use JNI to implement 2 // instance: 3 4 // create HelloWorld. java 5 class HelloWorld 6 {7 private native void print (); 8 public static void main (String [] args) 9 {10 new HelloWorld (). print (); 11} 12 13 static14 {15 System. loadLibrary ("HelloWorld"); 16} 17} 18 // pay attention to the print method declaration. The keyword native indicates that the method is implemented by a native code. In addition, pay attention to the System. loadLibrary call of the static code segment. This Code indicates that the libHelloWorld. so library is automatically loaded when the program is loaded. 19 // compile HelloWorld. java20 // run the following command in the command line: 21 javac HelloWorld. java22 // compile and generate HelloWorld. class in the current folder. 23 // generate HelloWorld. h24 // run the following command in the command line: 25 javah-jni HelloWorld26 // HelloWorld. h is generated in the current folder. Open HelloWorld. h, you will find the following code: 27/* do not edit this file-it is machine generated */28 # include <jni. h> 29/* Header for class HelloWorld */30 31 # ifndef _ Included_HelloWorld32 # define _ Included_HelloWorld33 # ifdef _ cplusplus34 extern "C" {35 # endif36/* 37 * Class: helloWorld38 * Method: print39 * Signature: () V40 */41 JNIEXPORT void JNICALL Java_HelloWorld_print42 (JNIEnv *, jobject); 43 44 # ifdef _ cpl Usplus45} 46 # endif47 # endif48 // The file contains the declaration of a function Java_HelloWorld_print. Here there are two parameters, which are very important and will be discussed later. 49 // implement HelloWorld. c50 // create HelloWorld. enter the following code in the c file: 51 # include <jni. h> 52 # include <stdio. h> 53 # include "HelloWorld. h "54 55 JNIEXPORT void JNICALL56 Java_HelloWorld_print (JNIEnv * env, jobject obj) 57 {58 printf (" Hello World! \ N "); 59} 60 // note that the jni. h header file must be included, which defines various types and macro definitions used by JNI. 61 // pay attention to the two Java_HelloWorld_print parameters. This example is simple and does not need to be used. However, these two parameters are very important in JNI. 62 // env represents the Java Virtual Machine environment. The parameters passed by Java are very different from those passed by c. You need to call the interface provided by JVM to convert it to C type, the env method is called to complete the conversion. 63 // obj indicates the called object, which is equivalent to this of c ++. When the c function needs to change the member variable of the called object, this object can be operated. 64 // compile and generate libHelloWorld. so65 // execute the following command in Linux to complete compilation: 66 cc-I/usr/lib/jvm/java-6-sun/include/linux/67-I/usr/lib/jvm/java-6-sun/include/68 -fPIC-shared-o libHelloWorld. so HelloWorld. c69 // generate libHelloWorld in the current directory. so. Note that the include directory of Java must be included (set according to your system environment), because Helloworld. c contains jni. h. 70 // another note is that in HelloWorld. java, the LoadLibrary method loads "HelloWorld", but the Library we generate is libHelloWorld. This is what Linux links stipulate. A library must be lib + Library name +. so. You only need to provide the database name for the link. 71 // run the Java program HelloWorld72 // The last step is to verify the previous result at: 73 java HelloWorld74 // if you encounter a problem in this step, if you receive java. lang. unsatisfiedLinkError is abnormal. You can specify the path of the shared library as follows: 75 java-Djava. library. path = '. 'helloworld76 // Of course, there are other ways to specify the path. For details, refer to using JNI on Linux. 77 // we can see the long-overdue "Hello world !" Output.View Code

After you try to complete the process, you have generated a com_test_GetMsg.h header file, and completed test. c to generate the libtest. so file. When JAVA calls the SO file, it repeatedly reports:

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 be loaded, which is valid.

Later, after comparing the instance, we found that the problem is that the file name of com_test_GetMsg.h is because com_test is the class name and GetMsg is the method name, but the method names are named like this:

1 1/* 2 2 * Class: com_test3 3 * Method: GetMsg4 4 * Signature: () V5 5 */6 6 JNIEXPORT jstring JNICALL Java_com_test_GetMsg7 7 (JNIEnv *, jobject );View Code

Here the problem arises. The '_' of the method name is a special symbol. In this way, when java loads the SO file, it is assumed that com is the class name and test is the method name, SO the load fails all the time. Okay, it's a careless disaster!

Regenerate the test. h file. The method name is as follows:

1 1/* 2 2 * Class: test3 3 * Method: GetMsg4 4 * Signature: () V5 5 */6 6 JNIEXPORT jstring JNICALL Java_test_GetMsg7 7 (JNIEnv *, jobject );View Code

The problem is solved!

I am a non-professional JAVA developer and cannot write well. Welcome to JAVA.


How can I use java to call the C language to write a dynamic link library? I just want to know the steps to solve this problem and what knowledge is needed

Jni technology required
 
When java calls the so dynamic library written in C language and lacks parameters, why can it still be called without prompting an error?

In the sub method in C, type the second parameter and check whether it is null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.