There doesn't seem to be an obvious way for specifying which ClassLoader gets used by JNI's FindClass(). I have a problem where I create a new native thread from a JNI native method and I want to call FindClass() within it, but the system classloader always gets used. The problem is that I am running withing Java Webstart and unless I use the JNLP ClassLoader (which I have a reference to) my application will not run properly.
- I have method JavaLoadClass() which loads the class from pure Java code - I have method NativeLoadClass() which is a native function called by a native thread and it attempts to load the class as well
- If I invoke JavaLoadClass() from a Java thread, then NativeLoadClass(), it works. - If I invoke NativeLoadClass() it fails - If I invoke JavaLoadClass from the native thread, then NativeLoadClass(), it still fails.
The problem seems to have more to do with the native thread than it does with JNI's FindClass(). My code invokes Thread.setContextClassLoader() to ensure that both the native and Java thread use the same class-loader and JavaLoadClass() uses Thread.currentThread.getContextClassLoader().loadClass() to load the class.
This behavior seems highly problematic to me because it implies that I have to predict ahead of time what classes my native code will be loaded and pre-loading them within some Java thread.
Joined: Dec 05, 2004
Ok, the situation is worse than I had originally thought. My previous post is incorrect.
I can't get FindClass() to work within the native code no matter what I do. I can invoke FindClass() within some Java native method, it works, then a couple of moments later I invoke the same thing from within a non-Java native method and then it fails.
When I say "Java native method" I am refering to a method that is defined using the "native" keyword and implemented in C++. Whereas "non-Java native method" is some function I declare within my C++ code that has no corresponding method in a Java class.
I am stuck...
subject: Specifying which ClassLoader gets used by FindClass()