Calling a static void Java method from JNI

Knossos

My C code does not manage to find the public static function call I have in Java. It manages to find the class without error.

I am attempting to return a result to a callback method onResponse. "5" will be replaced by something more complex later.

I have seem similar questions on StackOverflow, but no changes so far seem to have helped. Perhaps I have a logic error?

My JNI (updated #2):

void cec_debug_msg(JNIEnv *env, cec_rx_message_t* msg) {
    jclass cls = (*env)->FindClass(env, "com/example/utils/CECUtils");
    if(!cls) {
        LOGE("Could not find the CEC class.");
    } else {
        jmethodID methodid = (*env)->GetStaticMethodID(env, cls, "onResponse", "(I)V");
        if(!methodid) {
            // Code always reaches this point, never finding the method
            LOGE("Could not find the callback method.");
        } else {
            LOGV("Called static void method.");
            (*env)->CallStaticVoidMethod(env, cls, methodid, 5);
        }
    }
}

This code is contained in a method that is called from this function:

JNIEXPORT int JNICALL Java_com_example_utils_CECUtils_startListening(JNIEnv *env, jclass cls) {

    ...

    cec_debug_msg(env, &rx_msg);

    ...

}

My Java (updated #1):

public class CECUtils {

    static {
        System.loadLibrary("cecutils");
    }

    public static native int startListening();

    ...

    public static void onResponse(int opcode) {
        Utils.log("CEC", "From JNI!");
    }

}

Signature check:

javap -s -p CECUtils

public static void onResponse(int);

Signature: (I)V

Solution: Check ProGuard!

-keep public class com.example.utils.CECUtils {
    *;
}
Damien O'Reilly

Try using GetStaticMethodID instead of GetMethodID.

Note, you will need to call CallStaticVoidMethod instead of CallVoidMethod also.

See here for more info: http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Calling an overridden static method from parent

From Java

calling non-static method in static method in Java

From Java

calling static method in java

From Java

calling a super method from a static method

From Java

Calling Java Main method from static block

From Java

Call static Java method from separate thread using JNI

From Java

Java 8 method reference to static void method

From Dev

calling static method from inside the class

From Dev

Calling a static method from NSTimer. Is it possible?

From Dev

Efficiency of java static method calling

From Dev

Calling a static template method from a template function

From Dev

Calling a static method from a non-static context

From Dev

Java JNI NullPointerException after calling method from C with valid pointers

From Dev

JNI Calling Java Method With Array Parameter

From Dev

Calling a static method from a generic constraint Dart

From Dev

Calling a Java static method with generics from Scala

From Dev

Calling a non-static method return type void

From Dev

Calling a variable from a void method

From Dev

Calling super static method from designated initializer

From Dev

Calling non-static void from another cs file

From Dev

Calling a Java Method from the native code using jni

From Dev

Calling java function from c++ using jni: Failed to find static method id

From Dev

Calling java non-static method in Clojure

From Dev

Java - Go To Non-Static Void From Static Void?

From Dev

Calling a static method from cshtml file

From Dev

Calling non static method from static method USING instance created in the static method

From Dev

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

From Dev

Calling a non static method from a in a static context

From Dev

How to resolve a Bad global or local ref passed to JNI error when calling a java method from native code

Related Related

  1. 1

    Calling an overridden static method from parent

  2. 2

    calling non-static method in static method in Java

  3. 3

    calling static method in java

  4. 4

    calling a super method from a static method

  5. 5

    Calling Java Main method from static block

  6. 6

    Call static Java method from separate thread using JNI

  7. 7

    Java 8 method reference to static void method

  8. 8

    calling static method from inside the class

  9. 9

    Calling a static method from NSTimer. Is it possible?

  10. 10

    Efficiency of java static method calling

  11. 11

    Calling a static template method from a template function

  12. 12

    Calling a static method from a non-static context

  13. 13

    Java JNI NullPointerException after calling method from C with valid pointers

  14. 14

    JNI Calling Java Method With Array Parameter

  15. 15

    Calling a static method from a generic constraint Dart

  16. 16

    Calling a Java static method with generics from Scala

  17. 17

    Calling a non-static method return type void

  18. 18

    Calling a variable from a void method

  19. 19

    Calling super static method from designated initializer

  20. 20

    Calling non-static void from another cs file

  21. 21

    Calling a Java Method from the native code using jni

  22. 22

    Calling java function from c++ using jni: Failed to find static method id

  23. 23

    Calling java non-static method in Clojure

  24. 24

    Java - Go To Non-Static Void From Static Void?

  25. 25

    Calling a static method from cshtml file

  26. 26

    Calling non static method from static method USING instance created in the static method

  27. 27

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

  28. 28

    Calling a non static method from a in a static context

  29. 29

    How to resolve a Bad global or local ref passed to JNI error when calling a java method from native code

HotTag

Archive