JNI function error in android - java

I'm in trouble with this: http://imgur.com/hH0q3Tn
I'm using the VTK sample code and JNI isn't working and I don't know what to do. I compiled all the libraries and when I try the app on phone, the app stop.
What can I do?
Edit: Errors: http://pastebin.com/rf7vFVT8

The second arg for static native method should be jclass, not jobject.
See Are native Java methods equivalent to static Java methods? for more details.
Native Method Arguments
The JNI interface pointer is the first argument to native methods. The JNI interface pointer is of type JNIEnv. The second argument differs depending on whether the native method is static or nonstatic. The second argument to a nonstatic native method is a reference to the object. The second argument to a static native method is a reference to its Java class.

I solved.
The building of the library was incorrect and I followed this guide http://www.vtk.org/Wiki/VTK/Building/Linux

Related

pass C++ object from C++ to Java as function argument

I'd like to pass C++ object from C++ to Java as function argument. I use SWIG to generate Java bindings for my class definition(named "MyObj"). I use the following method(env is a JNIEnv*),
env->CallStaticVoidMethod(cls, mid, my_obj);
where "cls" corresponds to a class I get via "env->FindClass" and "mid" points to a static method of it. When I run the program, however, in that method("mid") I found that my argument is just a null pointer.
I also tried to use (jobject)(&my_obj) but it also doesn't work. Did I miss something? Thanks so much!

static block in java.lang.Object [duplicate]

In java, what does the private static method registerNatives() of the Object class do?
The other answers are technically correct, but not very useful for someone with no JNI experience. :-)
Normally, in order for the JVM to find your native functions, they have to be named a certain way. e.g., for java.lang.Object.registerNatives, the corresponding C function is named Java_java_lang_Object_registerNatives. By using registerNatives (or rather, the JNI function RegisterNatives), you can name your C functions whatever you want.
Here's the associated C code (from OpenJDK 6):
static JNINativeMethod methods[] = {
{"hashCode", "()I", (void *)&JVM_IHashCode},
{"wait", "(J)V", (void *)&JVM_MonitorWait},
{"notify", "()V", (void *)&JVM_MonitorNotify},
{"notifyAll", "()V", (void *)&JVM_MonitorNotifyAll},
{"clone", "()Ljava/lang/Object;", (void *)&JVM_Clone},
};
JNIEXPORT void JNICALL
Java_java_lang_Object_registerNatives(JNIEnv *env, jclass cls)
{
(*env)->RegisterNatives(env, cls,
methods, sizeof(methods)/sizeof(methods[0]));
}
(Notice that Object.getClass is not in the list; it will still be called by the "standard" name of Java_java_lang_Object_getClass.) For the functions listed, the associated C functions are as listed in that table, which is handier than writing a bunch of forwarding functions.
Registering native functions is also useful if you are embedding Java in your C program and want to link to functions within the application itself (as opposed to within a shared library), or the functions being used aren't otherwise "exported", since these would not normally be found by the standard method lookup mechanism. Registering native functions can also be used to "rebind" a native method to another C function (useful if your program supports dynamically loading and unloading modules, for example).
I encourage everybody to read the JNI book, which talks about this and much more. :-)
What might be slightly confusing is that the code shown for java.lang.Object.registerNatives in a previous answer is just an example of how to register native functions. This is the code that (in the implementation of OpenJDK) registers native functions for class Object. To register native functions for your own class, you must call the JNI function RegisterNatives from the native code in your own library. This might sound a bit circular, but there are a couple ways to break the loop.
Follow the example of this implementation of class Object:
a. In your Java class, declare a native method (preferably static) named registerNatives (or any other name. it doesn't matter).
b. In your native code, define a function named Java_<your fully qualified class name>_registerNatives, which contains a call to the JNI function RegisterNatives.
c. Make sure that in your Java code, your Java registerNatives method is called prior to any calls to other native methods.
OR
Use JNI_OnLoad
a. In your native library define a function jint JNI_OnLoad(JavaVM *vm, void *reserved). In the body of this function, call the JNI function RegisterNatives.
b. The Java VM will automatically look for and call JNI_OnLoad when your native library is loaded by System.loadLibrary, which you should already be calling, probably in a static initializer for your class. (You get the required env pointer by calling the GetEnv function in the table that the vm pointer points to.)
It is used on such scene :
C or C++ work as host, and Java the JVM as client. (C load jvm.dll and use JNI_CreateJavaVM to create a JVM)
When java code need to call host's C function,
If you still use jni dll (System.loadLibrary("foo.dll");) to bind the native java method, the memory space of dll is not the same as C host. (It still works if the function you called is host state irrelevant, but you cannot access host's state values in this way)
Here you need to use env->RegisterNatives() at host to inject(bind,expose,export) host's C functions to the JVM.

JNI GetObjectClass always returns java/lang/Class

env->CallVoidMethod is returning java/lang/Class when using
env->GetObjectClass(aobject); //aobject was the argument sent by JNI to C++
aobject is a Java object that implements an interface.
jobject obj = env->GetObjectClass(aobject)
is supposed to return the Java object but instead is returning java/lang/Class
I encountered this error and had to ask around at work before I got a real answer.
The problem is that when you designate your native method as static, it supplies an instance of the jclass, not a jobject instance of that class, as it's called from a static context. (If you call getCanonicalName() on that jclass, it will return your class's name.)
If the native method needs to be static, then you should pass in the instance as an argument if you need it. Otherwise, just make it not static and you should be all fixed.
You haven't regenerated your .h/.c file since you removed 'static' so your JNI method signature doesn't match the Java one. You have an extra jclass in the argument list that is only there for static methods. So you're going to get some very strange execution.
The answer to my problem was described in "The Java Native Interface - Programmer's Guide and Specification" by Shen Liang.
"You can use Call< Type >Method family of functions to invoke interface methods as well. You must derive the method ID from the interface type"

Calling java method from c++ code, without passing any JavaVm

I have class in C++ which must response for HTTP connection, that class must establish connection, send responses and do some other functions. This class is a part of huge project which I compile and finally get .so library for Android.
class HTTPSClient
{
public:
WinHTTPSClient();
~WinHTTPSClient();
bool Connect(const XMLString& a_strURL);
};
Now I want to call java functions from Connect function in c++. I have experiences in calling java functions from C++ code, but I always pass JavaVM to the class for example I call init(JavaVM* javaVm); and give as an argument javavm which comes from native code.
Now my question is: Can I call java method from C++ code without passing as an argument any JavaVm.
Define the JavaVM as a global/static variable of some class, e.g., by wrapping it up as a singleton. The variable can be accessed without explicitly passing it as an argument.
You can use the Java Invocation API to create a VM directly in your native code.
Jim S.

JNI and static interface

Is JNI's "method signature" different if method is defined to return (static) interface ?
In my Java class I have this method:
public SharedPreferences.Editor getSharedPrefsEditor() {
return mActivity.getPreferences(Context.MODE_PRIVATE).edit();
}
SharedPreferences.Editor is a static interface in SharedPreferences.
In my C++ JNI code I do this:
// 'env' is the java environment that JNI passes to us
// 'jObject' is the one that JNI passes to us (along with env)
jclass javaCallerClass = env->GetObjectClass(jObject);
jmethodID methodId_getSharedPrefsEditor = env->GetMethodID(
javaCallerClass,
"getSharedPrefsEditor",
"()Landroid/content/SharedPreferences/Editor;");
For some odd reason, this doesn't work. It compiles, but at runtime I get this:
DEBUG/dalvikvm(19020): GetMethodID:
method not found:
Lcom/mangotaster/madballs/MyRenderer;.getSharedPrefsEditor:()Landroid/content/SharedPreferences/Editor;
I'm calling other methods in that class in pretty much the same way without any problems.
The only change seems to be the return value.
I did try to call the edit() function straight from JNI code, but got the same error - which makes me believe that my function signature "()Landroid/content/SharedPreferences/Editor;" is indeed wrong.
More info on the SharedPreferences class.
Nested/Inner classes don't use the standard namespace nomenclature in JNI signatures. The inner class is actually translated to a normal class at the same level as the outer class by the Java compiler with the name "Outer$Inner". I think you want "()Landroid/content/SharedPreferences$Editor;".
No need to guess about this, or ask on forums ;-) javap -s will tell you the correct JNI signature string for any Java method.

Categories