Calling Java Methods from JNI results in program crash

arpit joshi

Calling get or set methods crashes. Object array is valid. It prints till line 2.Intially was getting error while inserting into the jobjectArray .Also tried with getting the value from Jobject .Both getter and setters fail .

JNICode is as below :

JNIEXPORT jint JNICALL Java_demo_JNIWrapper_pax_1store_1get_1data_1avail_1info
  (JNIEnv *env, jclass jclass1, jobjectArray jobj)

{

.....
.....

     int len = (*env)->GetArrayLength(env, jobj);

    printf ("Incoming object array length = %d\n", len);// - - - > Works.. shows 2 (I am passing 2 objects)

    jobject j = (*env)->GetObjectArrayElement(env, jobj,0);
        printf("This line 1 \n ");
       jmethodID meth1=(*env)->GetMethodID(env,jclass1,"getTimestamp","()Ljava/lang/String;");
printf("This line 2 \n ");    // - - - - > Works.. 

        jstring string_from_obj = (*env)->CallObjectMethod(env,j,meth1);   // - - - - > Crashes..
printf("This line 3 \n ");    // - - -> does not print this..
        printf("Contents are =%s\n",(*env)->GetStringUTFChars(env, string_from_obj, 0)); 

............

}       

My Java Object is as Below :

public class DataAvailable {
String timestamp;
public String getTimestamp() {
    return timestamp;
}
public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}
}

Call to JNI is as below :

public native int pax_store_get_data_avail_info(DataAvailable[] stats_array);
Wheezil

The glaring issue is that pax_store_get_data_avail_info() is not a method of DataAvailable. It is a method of some other class? You don't actually say what it is. But let's suppose it is:

class X {
   public native int pax_store_get_data_avail_info(DataAvailable[] stats_array);
}

When you call this method, the jclass jclass1 argument you get is for "X", not for DataAvailable. So your call to GetMethodID() probably fails. You don't check the return, so you re probably handing a bad method ID to the next call. JNI fails are hard crashes and very ugly.

You need to query the jclass for DataAvailable and use that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why calling methods from container class instead of instances of it in Java

From Java

Calling into a saved java object via JNI from a different thread

From Java

Calling a mapreduce job from a simple java program

From Java

Calling Java Methods from Shell Scripts

From Java

Calling multiple methods in Java

From Java

Calling a Java Program from a Cocoa Application

From Java

Can't call java methods from c++ wrapper in JNI

From Java

How To Call JAVA Methods from inside of a Thread in JNI

From Java

Calling the methods in a class in Java

From Java

Keeping java methods called from Android JNI

From Dev

Calling a static void Java method from JNI

From Dev

Java JNI NullPointerException after calling method from C with valid pointers

From Dev

androidx renderscript crash "Error loading RS jni library: java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad"

From Dev

calling methods from a chain inheritance program

From Dev

JNI calling Java from C++ with multiple threads

From Dev

Calling Java Methods in XSLT

From Dev

XPages - Calling Java methods

From Dev

Calling a Java Method from the native code using jni

From Dev

calling a java program from CentOS 7 terminal

From Dev

JNI vs JNA, calling Java from Fortran95

From Dev

Calling the methods in java

From Dev

Program crash when calling OpenGL functions

From Dev

JNI: Calling a java method from C periodically is not working

From Dev

Proper/easy way of calling methods from a program on remote server in java?

From Dev

Calling RenderScript from C / JNI

From Dev

Calling a java program from a kernel mode driver

From Dev

Calling methods on objects Java

From Dev

Java JNI call FindClass fails from a C++ program

From Dev

calling objects from other methods in java

Related Related

  1. 1

    Why calling methods from container class instead of instances of it in Java

  2. 2

    Calling into a saved java object via JNI from a different thread

  3. 3

    Calling a mapreduce job from a simple java program

  4. 4

    Calling Java Methods from Shell Scripts

  5. 5

    Calling multiple methods in Java

  6. 6

    Calling a Java Program from a Cocoa Application

  7. 7

    Can't call java methods from c++ wrapper in JNI

  8. 8

    How To Call JAVA Methods from inside of a Thread in JNI

  9. 9

    Calling the methods in a class in Java

  10. 10

    Keeping java methods called from Android JNI

  11. 11

    Calling a static void Java method from JNI

  12. 12

    Java JNI NullPointerException after calling method from C with valid pointers

  13. 13

    androidx renderscript crash "Error loading RS jni library: java.lang.UnsatisfiedLinkError: JNI_ERR returned from JNI_OnLoad"

  14. 14

    calling methods from a chain inheritance program

  15. 15

    JNI calling Java from C++ with multiple threads

  16. 16

    Calling Java Methods in XSLT

  17. 17

    XPages - Calling Java methods

  18. 18

    Calling a Java Method from the native code using jni

  19. 19

    calling a java program from CentOS 7 terminal

  20. 20

    JNI vs JNA, calling Java from Fortran95

  21. 21

    Calling the methods in java

  22. 22

    Program crash when calling OpenGL functions

  23. 23

    JNI: Calling a java method from C periodically is not working

  24. 24

    Proper/easy way of calling methods from a program on remote server in java?

  25. 25

    Calling RenderScript from C / JNI

  26. 26

    Calling a java program from a kernel mode driver

  27. 27

    Calling methods on objects Java

  28. 28

    Java JNI call FindClass fails from a C++ program

  29. 29

    calling objects from other methods in java

HotTag

Archive