Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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

Specifying which ClassLoader gets used by FindClass()

Gili Tzabari

Joined: Dec 05, 2004
Posts: 16

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?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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

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 Tzabari

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

I agree. Here's the link:
subject: Specifying which ClassLoader gets used by FindClass()
It's not a secret anymore!