Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

Custom ClassLoader - changing order of loading to look in my loader first

Thread view: 
amw - 24 Sep 2003 22:56 GMT
Problem: my J2EE ear file uses a third-party library (bunch of jar
files) to do SNMP stuff.  However, the J2EE vendor also uses this
third-party library to do its own SNMP stuff.  The problem is the vendor
uses a different version of the third-party library, so my code tries to
run and finds the wrong version of the library (of the jar files) that
the J2EE vendor has supplied.

Possible solution: I plan to have a custom classloader (derived from
java.lang.ClassLoader) that gets hooked in to my threads using
Thread.setContextClassLoader().  This custom classloader would look for
the third-party library jars in a spot I specify so it would find the
correct ones.  Since I want to change the usual classloader behavior of
"look in parent classloaders first, then child classloader" to "look in
child classloader first, then in parent classloaders" I can't just
override ClassLoader.findClass().  I, at a minimum, have to override
ClassLoader.loadClass() to look in my classloader first.  I have
questions about this:

1. What other methods do I have to override?  For example, which of the
resource-related methods do I need to override so that resources are
searched for first in my classloader then in parent classloaders?

2. I was thinking of using a URLClassLoader as a helper to my
classloader, invoking its method(s) from my custom classloader's
method(s) to actually load the classes from URLs that are not on the
standard classpath.  I was planning to set it up with a custom parent
classloader that can't find anything, so that the helper URLClassLoader
would only ever find classes/resources in the URLs I provide to it.
Does this approach make sense?  Have you seen anything like this done
before?

Thanks in advance for any help you can provide.
Chris Smith - 25 Sep 2003 04:16 GMT
> Possible solution: I plan to have a custom classloader (derived from
> java.lang.ClassLoader) that gets hooked in to my threads using
[quoted text clipped - 10 lines]
> resource-related methods do I need to override so that resources are
> searched for first in my classloader then in parent classloaders?

Looks like loadClass, getResource, and getResourceAsStream would be good
enough.  You should be aware that this non-standard order can cause
problems unless usage of the classes to be loaded by your out-of-order
loader is sufficiently isolated.  That is, it's problematic if:

ClassLoader1 is the system classloader.
ClassLoader2 has ClassLoader1 as a parent.
However, ClassLoader2 checks for ClassX elsewhere before delegating to
ClassLoader1.

ClassLoader1 loads ClassA.
ClassLoader2 loads ClassB.
ClassA has a method foo(ClassX).
ClassB wants to call ClassA's foo(ClassX) method.

However, as long as usage of ClassX is isolated across that boundary,
you can avoid the problem.  Just make sure you understand and document
this limitation with regard to the class library that is loaded out-of-
order.

> 2. I was thinking of using a URLClassLoader as a helper to my
> classloader, invoking its method(s) from my custom classloader's
[quoted text clipped - 4 lines]
> Does this approach make sense?  Have you seen anything like this done
> before?

No, I wouldn't do it like that.  I'd set up a URLClassLoader as a
"friend" classloader, and write a new sort of composition classloader
that delegates to its friend, and then to its parent only if the friend
can't help... kinda like your average adolescent.  So there'd be three
ClassLoaders in all, but the actual ClassLoader that you write would
only exist to delegate to others.  Does that make sense to you?

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

gary_shank@stercomm.com - 14 Nov 2005 20:41 GMT
All I want to do is programatically override the classloader (without
specifying -Djava.system.class.loader) so that it calls my classloader if it
can't find a particular class.  No matter what I've tried - it doesn't work.
For instance, Thread.setContextClassLoader doesn't do it.  I can explicitly
create a class using my classloader with Class c = Class.forName("Foo", true,
loader), but as soon as I try doing Foo foo = (Foo)c.newInstance() - I get
NoClassDefFoundError yet my classloader's loadClass nor findClass methods
were called.  I thought, from all the various articles I've read, that I
should be able to hook my classloader into this elaborate chain so that I can
try to resolve anything that the parents can't.  Basically, I'm trying to do
the class loading of jar's within a jar.  I'm aware of the One-Jar stuff but
I'm trying to do this without any 3rd party code.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.