Call a static java method of another package from native code

Tom :

For example, let's say that in Android, I need to call the static method android.os.SystemClock.elapsedRealtime(), which returns a long, from a portion of native code. In the mylib.c file, I have

JNIEXPORT jlong JNICALL 
Java_com_mpackage_MyClass_nativeMethod(JNIEnv *env, jobject obj){

  jclass cls = (*env)->GetObjectClass(env, obj);
  jmethodID mid = (*env)->GetStaticMethodID(env, cls, "android.os.SystemClock.elapsedRealtime", "(V)J");

  if (mid == 0)
    return 0L;

  return CallStaticLongMethod(cls, mid);
}

In the java MyClass.class, I have among others

static {System.loadLibrary("myLib");}
native long nativeMethod();

but when I call it, I get the following error:

ERROR/AndroidRuntime(628): java.lang.NoSuchMethodError:
android.os.SystemClock.elapsedRealtime()

at the declaration of mid line. I think this is straightforward but I'm new to jni.

Can someone point out my mistake(s)?

bobby :

Looks like your usage of the JNI API is not proper. First you should get the class reference of android.os.SystemClock. The obj passed as a parameter, is an object of MyClass. You should use (*env)->FindClass(env, "android/os/SystemClock") to get a jclass for the SystemClock. Then call (*env)->GetStaticMethodID(env, cls,"elapsedRealtime", "(V)J"); to get the method id. Take a look at the JNI tutorial for further details

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Call method of class from another package without instantiating the class

From Java

Call Kotlin object with class delegation from Java as a static method

From Java

Send static methods as parameter and generically call within another method in Java

From Java

How to call getClass() from a static method in Java?

From Java

Static methods - How to call a method from another method?

From Java

Java: how to call non static method from main method?

From Java

access java synchronized method from native code

From Java

Why can't I call a protected method from an inheriting class in another package in Java?

From Java

Call static Java method from separate thread using JNI

From Dev

What guarantees that this call to a static method from another class's static block works as expected?

From Dev

Scala 2.12.4: Cannot access protected static Java method from another package anymore

From Dev

How to call a static method from another static method in React?

From Dev

scala - How to call public static method defined in Java from Scala?

From Dev

Call static method from a header inside another header

From Dev

Unable to call Method from another class in React Native

From Dev

Is it possible to call a static method from withing another static method

From Dev

How to call non-static java method from c++?

From Dev

How to call a method from another class, which is in another .java file?

From Dev

Why not call the static method "main" of a class from another class?

From Dev

PHP: is it possible to call a static class method from another static class?

From Dev

Call non static method from another page

From Dev

Call PanResponder methods from another method in React Native

From Dev

Move method to another Java class, and call the method from the original class

From Dev

Unable to call method from another method of the same class in JAVA

From Dev

Call static method from another class without creating object in Java

From Dev

Test static method that call another from same class

From Dev

Call static method from constructor in Java

From Dev

how to call a static method from another class in JS

From Dev

Freemarker call static java method from java.lang.Integer

Related Related

  1. 1

    Call method of class from another package without instantiating the class

  2. 2

    Call Kotlin object with class delegation from Java as a static method

  3. 3

    Send static methods as parameter and generically call within another method in Java

  4. 4

    How to call getClass() from a static method in Java?

  5. 5

    Static methods - How to call a method from another method?

  6. 6

    Java: how to call non static method from main method?

  7. 7

    access java synchronized method from native code

  8. 8

    Why can't I call a protected method from an inheriting class in another package in Java?

  9. 9

    Call static Java method from separate thread using JNI

  10. 10

    What guarantees that this call to a static method from another class's static block works as expected?

  11. 11

    Scala 2.12.4: Cannot access protected static Java method from another package anymore

  12. 12

    How to call a static method from another static method in React?

  13. 13

    scala - How to call public static method defined in Java from Scala?

  14. 14

    Call static method from a header inside another header

  15. 15

    Unable to call Method from another class in React Native

  16. 16

    Is it possible to call a static method from withing another static method

  17. 17

    How to call non-static java method from c++?

  18. 18

    How to call a method from another class, which is in another .java file?

  19. 19

    Why not call the static method "main" of a class from another class?

  20. 20

    PHP: is it possible to call a static class method from another static class?

  21. 21

    Call non static method from another page

  22. 22

    Call PanResponder methods from another method in React Native

  23. 23

    Move method to another Java class, and call the method from the original class

  24. 24

    Unable to call method from another method of the same class in JAVA

  25. 25

    Call static method from another class without creating object in Java

  26. 26

    Test static method that call another from same class

  27. 27

    Call static method from constructor in Java

  28. 28

    how to call a static method from another class in JS

  29. 29

    Freemarker call static java method from java.lang.Integer

HotTag

Archive