Data interaction between java and c

Data interaction between Java and C/C++ using JNI

I recently worked on a TIemsten database project that involved using JNI (Java Native Interface) technology. In this project, we developed the user interface and business logic in Java, while the ODBC access layer was implemented in C. Although simple ODBC operations are not complicated, transferring data between Java and C can be quite challenging. There are several common scenarios for data exchange between Java and C, such as passing basic data types, transferring Java objects to C, returning Java objects from C, and even invoking Java classes from C. Below is a detailed explanation of these cases.

1. Passing Basic Data Types from Java to C

For basic data types, Java and C have direct equivalents, so they can be used directly. Here's a mapping of the corresponding types:

  • Java Type → C Type
  • byte → jbyte (8-bit signed)
  • boolean → jboolean (8-bit unsigned)
  • short → jshort (16-bit signed)
  • int → jint (32-bit signed)
  • long → jlong (64-bit signed)
  • float → jfloat (32-bit float)
  • double → jdouble (64-bit double)
  • char → jchar (16-bit unsigned)
  • void → void

These mappings allow for straightforward data transfer between Java and C when dealing with primitive types.

2. Passing Java Objects to C

When a Java object is passed to C via JNI, the C code needs to load the class, obtain the method IDs, and then call the methods of the Java object. For example, if a Java `Customer` object is passed to a native function, and this object has a method `String getName()`, the C code can retrieve the method ID and invoke it as follows:

JNIEXPORT jobject JNICALL Java_com_oracle_estt_sc_db_impl_SCQueryODBC__1getCustomer
(JNIEnv *env, jobject, jobject customer) {
    jmethodID methodId;
    jclass cls_objClass = env->GetObjectClass(customer);
    methodId = env->GetMethodID(cls_objClass, "getName", "()Ljava/lang/String;");
    jstring js_name = (jstring)env->CallObjectMethod(customer, methodId, NULL);
    // Process the returned string
}

This allows for seamless interaction between Java objects and native code.

3. Returning Java Objects from C

When returning a Java object from C, the native code must first create an instance of the desired Java class, get the field IDs, set the values, and then return the object. For example, if we need to return a `Customer` object with attributes like `name`, the C code would look like this:

JNIEXPORT jobject JNICALL Java_com_oracle_estt_sc_db_impl_SCQueryODBC__1getCustomer
(JNIEnv *env, jobject, jobject customer) {
    jclass clazz = env->FindClass("com/oracle/estt/sc/busi/Customer");
    if (clazz == 0) return 0;

    jobject obj = env->AllocObject(clazz);
    jfieldID fid_id = env->GetFieldID(clazz, "customerID", "I");
    jfieldID fid_name = env->GetFieldID(clazz, "name", "Ljava/lang/String;");

    if (fid_id == 0 || fid_name == 0) return 0;

    env->SetIntField(obj, fid_id, 1);
    env->SetObjectField(obj, fid_name, jname);

    return obj;
}

This approach ensures that the Java object is correctly constructed and returned from the native code.

4. Passing Java Object Arrays to C

When passing arrays of Java objects to C, the native code must first determine the array size, iterate through each element, and process the objects. For example, if a Java array of `CustomerRequest` objects is passed to a native function, the C code could extract each object and process its properties:

JNIEXPORT void JNICALL Java_com_oracle_estt_sc_db_impl_SCInsertODBC__1insertCustomeRequest___3Lcom_oracle_estt_sc_busi_CustomerRequest_2
(JNIEnv *env, jobject, jobjectArray oa) {
    int size = env->GetArrayLength(oa);
    for (int i = 0; i < size; i++) {
        jobject o_customer = env->GetObjectArrayElement(oa, i);
        // Process the customer object
    }
}

This allows for efficient handling of complex data structures in JNI.

In conclusion, JNI provides a powerful mechanism for integrating Java and C/C++ code. While the process can be complex, understanding the different data transfer scenarios and following best practices can greatly simplify development and improve performance in mixed-language applications.

ADSS Fiber Optic Cable

Adss Fiber Optic Cable,adss cable,cable adss,Singlemode Adss Fibre Optics,Adss Fiber Cable

Guangzhou Jiqian Fiber Optic Cable Co.,ltd , https://www.jqopticcable.com