Creating your own custom classloader is an advanced subject. I've never had to write my own classloader. But I can imagine it could be necessary when you want to load classes in a nonstandard way, or if you need to control certain aspects such as to which libraries the loaded classes have access.
Tomcat for example has its own custom classloader for web applications, which gives each webapp access to the common Tomcat libraries and to the classes in the WEB-INF/classes directory and the libraries in the WEB-INF/lib directory of the webapp.
Originally posted by Jesper Young: Creating your own custom classloader is an advanced subject
Nah, it's not that hard. Well, not necessarily - it depends what you want your custom classloader to do.
The reasons I've had to write custom class loaders are: -
to fetch the class data from an unusual place, such as a SQL database
to apply simple encryption/decryption, as a deterrent to hacking and reverse engineering
I'm sure there are other reasons, too.
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Joined: Mar 22, 2005
I've used them to load classes from places outside of the classpath (e.g. a plugins directory for user-added classes), and for having a security manager associated with those classes, so that they're not allowed everything the hosting application is allowed to do.
Was the original question about writing your own ClassLoader class or using custom instances of the existing classes? I've never had to write one, but have made new URLClassLoader instances with modified classpaths.
JUnit uses a classloader trick to reload classes every time you run a test. I don't know if they have custom code or just an instance of URLClassLoader. It's Open Source so you can go take a look.
I used a monitoring tool that inserts bytecode at class load time. They "log" every method entry and exit so they can report invocation counts and performance.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi