0
votes

I have a custom URLClassLoader which loads a couple of classes from jar files outside the normal classpath. So far, so good.

My problem is that I can't call this ClassLoader each and every time when I need a class loaded by my ClassLoader because these classes might by used by third party libs. For that reason I added the classes to the current Thread ClassLoader by reflection:

    ProtectionDomain pd = getClass().getProtectionDomain();
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    final java.lang.reflect.Method clM = ClassLoader.class.getDeclaredMethod("defineClass", Class.class);         
    clM.invoke(classLoader, className, byteContent, 0, byteContent.length, pd);

But when it comes to a call to findClass inside that Thread ClassLoader and the requested class has not been added by me yet, I get a NoClassDefFoundException. Of course.

My question is now, is there a way to put my ClassLoader in the global class loading chain of my application? Using the Java option for system ClassLoader is not possible as my ClassLoader can be contained in a web application and deployed as a war file. I also tried using Thread.currentThread().setContextClassLoader() but once a new Thread is created, it of course doesn't have my ClassLoader set.

I searched the web for days but couldn't find any solution.

Thanks for your help and any suggestions.

Best regards, Gerry

1
You can set the parent class loader for your custom class loader to the System Class Loader. That will make it part of the class loader hierarchy. I have implemented a similar thing; but i am not sure you are explaining your problem precisely - java_geek

1 Answers

0
votes

Not really, especially if you want this to be compatible with running as a web application.

You've got multiple problems to solve here. Web containers are permitted to run you under a security manager that will make your code not work (i.e., they can forbid access to the filesystem and/or forbid the creation of ClassLoaders). Java was designed to allow the safe loading of mobile code (it was originally targeting, of all things, cable set-top boxes), and so you can imagine the ability for an application to modify the system class loader on the fly was not part of the architecture.

For the application, what you ought to be doing is either (1) putting your JAR files from which you're specially loading these classes in the system classpath, or (2) loading your application in a custom class loader, then having that custom class loader load your classes (in essentially the same way you are now). Load the third-party libraries in the same class loader. Now you're going to be OK, regardless of what you mean by "might be used by third-party libraries" (this was confusing to me and I am not sure what technique they might be using, and it matters).

For the webapp, you should bundle the needed classes in the web application. You're defeating the web application design by attempting to load classes from elsewhere; the idea of the web application and .war is to make these things self-contained.

So you're doing this the wrong way. If you insist on doing it the wrong way, there may be various non-portable hacks you can start using in order to get your code working properly in particular situations, but I'm only going to write one answer for now and see what you think. :)