File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Specifying which ClassLoader gets used by FindClass() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Specifying which ClassLoader gets used by FindClass()" Watch "Specifying which ClassLoader gets used by FindClass()" New topic
Author

Specifying which ClassLoader gets used by FindClass()

Gili Tzabari
Greenhorn

Joined: Dec 05, 2004
Posts: 16
Hi,

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.

Any ideas?
Gili
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

I don't know this works for sure with JNI, but I'm pretty sure that calling the Thread.setContextClassLoader() method will do what you want. This is how you accomplish the same task from Java code.


[Jess in Action][AskingGoodQuestions]
Gili Tzabari
Greenhorn

Joined: Dec 05, 2004
Posts: 16
I should clarify:

- 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.

Gili
Gili Tzabari
Greenhorn

Joined: Dec 05, 2004
Posts: 16
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...

Gili
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Specifying which ClassLoader gets used by FindClass()
 
Similar Threads
Own URLClassloader...
Custom ClassLoader which depends on more than one classloader
loadClass, defineClass (run-time loading)
Why getting ClassCastException?
Problem using custom ClassLoader