This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes Problem with web application Classloader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with web application Classloader" Watch "Problem with web application Classloader" New topic

Problem with web application Classloader

Francesco Marchioni
Ranch Hand

Joined: Sep 22, 2003
Posts: 194
Hi all,
I have a web application which uses several Utility classes
for business tasks. I'm going to distribute this web application
but I don't want to distribute the byte codes of the Utility classes.

So I've crypted the class and built a custom classloaders
which decrypts the bytes and load the class. So far so good.

Now the matter is: all in the code I have hardcoded the Utility Classes,
while on the other hand I simply have an Object in return from


How can have my classes loaded with a custom classloader without having to rewrite all the code ? (for example using reflection)

I thought to write a common superclass with a factory method which returns instances of the Utility classes.

public class Utility extends Master

void secretMethod() {


public class Master

getUtilityInstance() {
return new Utility();

void secretMethod() {


But then the "Master" class too must be loaded with the same classloader or the Utility class of the web application won't see it. But if the "Master"
class is not loaded from the standard web classloader how can I compile the code, when "Master" is used ? It's like the dog who bites its tail!

How can I solve the problem ?
Thanks a lot

Enterprise Integration Tutorials - JBoss-WildFly Tutorials
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Yopu should be able to have two JARs, one encrypted and loaded by your custom ClassLoader, and the second unencrypted and loaded by the standard WebApp ClassLoader (ie placed in the WEB-INF/lib directory.

The second JAR can contain Interfaces implemented by you classes, and you can cast happily as long as you construct your custom ClassLoader as a child of the web application ClassLoader. In your Servlets you can put ClassLoader loader = new CustomClassLoader(Servlet.class.getClassLoader()), but there may be a cleaner way to do that bit.

Usinf Interfaces rather than 'Master' classes is easier since you only need a type for the compiler, not actually any concrete implementations.
Francesco Marchioni
Ranch Hand

Joined: Sep 22, 2003
Posts: 194
Thanks for your answer. It was quite clear.
Regards Francesco
It is sorta covered in the JavaRanch Style Guide.
subject: Problem with web application Classloader
It's not a secret anymore!