0

How to access a C++ method from a static method of Java/Kotlin.

When i try to access it. I'm getting this error.

"Non-static 'stringFromJNI()' cannot be referenced from a static context"

JNIHelper.kt :

object JNIHelper {

external fun stringFromJNI(): String

init {
    System.loadLibrary("native-lib")
}

}

native-lib.cpp :

extern "C" JNIEXPORT jstring JNICALL
Java_com_my_package_JNIHelper_stringFromJNI( //Test
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

Sample.Java :

private static Data getData(Context context) {
     return JNIHelper.stringFromJNI(); //Here i get Compilation error
}

1 Answers1

-1

The same as you do in Java: you have to pass THE object, then get its Class, do a "env->getDeclaredMethod(object_class, "method_name", params)" to get the non-static method and then Call it.

In your case you have to do..:

JNIHelper().stringFromJNI()

.. because the Method belongs to a Class that should be instantiated, or you have to declare "stringFromJNI()" as Static, infact in my projects I declare JNI methods like

public native static void methodName();

and they could be used EVERYWHERE just by use the Class instead of its Instance.

emandt
  • 2,547
  • 2
  • 16
  • 20
  • That doesn't explain the error he is getting. – user207421 Jul 03 '19 at 08:24
  • @user207421 I see you have marked as "duplicated" but in the link you set there ISN'T ANYTHING about JNI, so your "marked as duplicated" flag doesn't solves the problem. In my answer I wrote HOW to call an Instance's Method while in a Static context. – emandt Jul 03 '19 at 08:37
  • It's not a JNI problem. He is getting this compiler error from his Java code. You can't call a non-static method from a static context, as per the duplicate, and when you do the compiler gives this error message, also as per the duplicate. If it was a JNI problem there would be an exception. – user207421 Jul 03 '19 at 10:25
  • @user207421 he has created "stringFromJNI()" function in C/C++ using JNI and he have tried to call it OUTSIDE the object-container/owner of this method. It's not a compilation error but a Runtime Exception. Please try yourself: try to execute a Class's method without "invoke" the method from its object but only from its Class, then you will get a RUNTIME Exception because from Java he just try to execute "stringFromJNI" that belongs to a Class and it's not a static method. However the title says "JNI", the author's description shown JNI code...I don't understand why you should negate it. – emandt Jul 03 '19 at 17:21
  • If you try what you suggest I should try you will get a compilation error from the Java code that calls the native method, as in the duplicate. You seem to be suggesting that he is calling this code from C++ via JNI. There is no evidemce of that question, and exactly no reason to do so, as you could call it directly. – user207421 Jul 04 '19 at 00:22
  • @user207421 Yes i get the compilation error only. But the solution is related to JNI. Why did you marked it as duplicate? – Bilal Hussain Jul 04 '19 at 10:12