N
N
nick0x012013-03-14 20:09:12
Java
nick0x01, 2013-03-14 20:09:12

Trouble in getting Java classes of a java library in native code using Jni?

Actually given:
1. Java-library with the implementation of the desired functionality. (including, it has several classes whose methods are marked as native).
2. A library on the pluses that uses the functionality of a java-lib and implements native methods from it. All this using Jni.
3. There is an android application in which you need to use all this.

The Java lib was included in the eclipse project, and in the java code of the application, I can instantiate and use class objects from the j-library. But I can't use these classes in native code - the classes are simply not found (FindClass returns NULL). Jni-dependent code works (for example, I can reach MainActivity and use it in native). Jni was initialized from MainActivity.
Perhaps this is due to the fact that the classes were loaded by different ClassLoaders (as far as I imagine this process in Java).
Who has already faced such a problem? Maybe there is a solution to this problem?
I really don't want to transfer the initialization of jni to the java library from the code of the application itself (because this will create a dependency of the java library on the plus code).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
makkarpov, 2013-03-15
@makkarpov

It is quite possible that because of the ClassLoader, because this manifests itself even without JNI. In Java code, such a class will also not be found until ClassLoader is explicitly specified (Class.forName("package.Class", true, myLoader)).
If I'm not mistaken, then System.loadLibrary() loads the library in the context of the classloader that loaded the class that called this function. So it's quite possible that your Jlib and your main code were loaded from different classloaders, and System.loadLibrary was called from a classloader with code - then the library will not find JNI methods, and JNI will not find the library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question