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