Data interaction between java and c

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

I recently worked on a Tiemsten database project that utilized JNI technology. In this project, we used Java to develop the interface and business logic, while C was used for the ODBC database access. Although simple ODBC is not difficult, transferring data between Java and C can be quite complex. There are several scenarios involved in data exchange between the two languages: passing basic data types, transferring Java objects to C, returning Java objects from C, and invoking Java classes from C. Below are detailed explanations of these cases.

1. Passing Basic Data Types from Java to C

For basic data types, Java and C have direct equivalents. Here is a mapping of common types:

Java Type | JNI Type | Size (bits)

boolean | jboolean | 8 (unsigned)

byte | jbyte | 8

char | jchar | 16 (unsigned)

short | jshort | 16

int | jint | 32

long | jlong | 64

float | jfloat | 32

double | jdouble | 64

void | void | N/A

2. Passing Java Objects to C

When passing Java objects to C, it's necessary to load the corresponding class, obtain method IDs, and then invoke the methods. For example, if a `Customer` object is passed from Java to C, and it has a method like `getName()`, you can retrieve its value 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);

// ... further processing

}

3. Returning Java Objects from C

In C, to return a Java object, you need to create it first, set its fields, and then return it. For instance, if you want to return a `Customer` object with a name attribute, you would do something 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;");

env->SetIntField(obj, fid_id, 1);

env->SetObjectField(obj, fid_name, jname);

return obj;

}

4. Passing an Array of Java Objects to Java

When dealing with arrays of Java objects, you first determine the array size, iterate through each element, and process them accordingly. For example, if a Java array of `CustomerRequest` objects is passed to C, you can loop through the array and extract individual values:

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

}

}

These examples demonstrate how Java and C interact via JNI, covering the most common data transfer scenarios. Understanding these concepts is essential when working with native code integration in Java applications.

Figure 8 Fiber Optic Cable

Figure 8 Fiber Optic Cable,Communication Cable,Outdoor Optical Cable,Aerial Cable

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