Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Specifying which ClassLoader gets used by FindClass()

 
Gili Tzabari
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gili Tzabari
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic