JNI: From C code to Java and JNI


I am developing an application for android in eclipse and now I have a problem and I need your help. So I must Call function written in C from JAVA application. But on my way of writing code I have some Questions which you can see below. I am waiting for your answers and ideas …

C Code:

typdef struct blobData_s {
    unsigned long length;
    unsigned char data[1];
} blobData_t;

unsigned int CheckEnrollmentExist ( unsigned long hdevice, blobData_t* pInputInfo ) {
    // Function code goes here
    return some_value;

JAVA Code:

In JAVA code instead of unsigned long I use int so I can write.

  • Picasso java.lang.IllegalStateException: Method call should not happen from the main thread
  • How to use fast scroll in android?
  • How to set up ADB for remote machine development and local device deployment
  • Android ViewPager with bottom dots
  • How to make an intent with multiple actions
  • GCM msg delivery times are WILDLY erratic
  • class jblobData_c {
        public int langth;
        *Question 1.*
    public class ApplicationMainClass extends Activity {
        // Some code goes here
        public native int JCheckEnrollmentExist( int jhdevive, *Question 2.* );

    Question 1.

    • What I can use instead of unsigned char in JAVA Code ?
    • What I must write in JAVA code instead of unsigned char data[1]; ?

    Question 2.

    • How I can use class jblobData_c instead of blobData_t* pInputInfo in the JAVA Code ?
    • What I must write in JAVA instead of blobData_t* pInputInfo ?

    JNI Code:

    JNIEXPORT jint JNICALL Java_com_Test_JCheckEnrollmentExist(JNIEnv* env, jobject obj, jint jhdevice, *Question 2.* ) {
        // Call the base function from C code.
        return CheckEnrollmentExist( jhdevice, *Question 3.*);

    Question 3.

    • What I must write in CheckEnrollmentExist function that is C Code Function instead of blobData_t* pInputInfo in order this function works right and given parameter be the same


    1. How to pass C structs back and forth to Java code in JNI ?
    2. Passing large C structure through JNI efficiently
    3. Return a Structure Object from C to Java Through JNI
    4. Pass data between Java and C
    5. Passing a pointer from JNI to Java using a long
    6. Passing pointers between C and Java through JNI

  • Android Notification.Builder: show a notification without icon
  • How to use Google Play Services 6.5 granular dependency management
  • Open PDF in android app
  • onActivityResult() not called in new nested fragment API
  • Android Sherlock ActionBar Up button
  • Android ACRA with handled exceptions
  • One Solution collect form web for “JNI: From C code to Java and JNI”

    For question #1:

    You can use a jchar. Primitive chars in java are not signed, it’s about the only primitive that isn’t. Note that jchar is a UTF-16 char, so you will have to “map” the jchar to a regular char, as you would have to with any character conversion issue. For simple conversions, this can typically be done by casting

    char c_char = (char)java_char;

    because the core ASCII shares the same numeric values between ASCII and UTF-16. However, this is prone to error should anyone actually attempt to pass a “special” character through the interface. A much better way would be to (in the java side, as it is easier) convert the characters to bytes using the appropriate character set for your platform (to ensure platform compatibility in the C layers). Then you only need to pass a byte[] to the JNI call, and the bytes will correctly correspond to the characters that C likely will expect.

    For question #2:

    If your CheckEnrollmentExists(...) method is the JNI binding entry point, you cannot change data types safely. That means that all entry inputs must be JNI data type values. While you might be able to select the C data type equivalents (and you might be able to get your compiler to do it anyway) such techniques should be frowned upon. This implicitly means that JNI entry points cannot accept struct data structure not defined in the JNI headers. In other words, you can’t pass your own struct to the method.

    If the method needs access to a C struct across calls, use another means. I’ve seen people store the pointer to the allocated data structure in a member integer or long (doing correct casting). You can then rewrite the native code side to retrieve the pointer from the “this” object being passed into the call, and the do a dereference to obtain the required data.

    For Question #3:

    This is actually the same as question #2. In the “binding wrapper” you put, you would retrieve the pointer’s stored value in the java object’s int or long field, cast it to the appropriate struct pointer and then pass it to the internal method. As the passing of the pointer is a C to C call, no extra magic is required.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.