As you have seen in this short tutorial, knowing how to create a custom ClassLoader can really help you get at the guts of the JVM. The ability to load class files from any source, or even to generate them on the fly, can extend the reach of your JVM and allow you to do some really interesting and powerful things.
As I mentioned earlier in this tutorial, custom ClassLoaders are crucial to programs like Java-enabled browsers and appletviewers. Here are a few other ideas for interesting ClassLoaders:
Security. Your ClassLoader could examine classes before they are
handed off to the JVM to see if they have a proper digital signature.
You can also create a kind of "sandbox" that disallows certain kinds
of method calls by examining the source code and rejecting classes
that try to do things outside the sandbox.
Encryption. It's possible to create a ClassLoader that decrypts
on the fly, so that your class files on disk are not readable by
someone with a decompiler. The user must supply a password to run the
program, and the password is used to decrypt the code.
Archiving. Want to distribute your code in a special format or with
special compression? Your ClassLoader can pull raw class file bytes from any source it wants.
Self-extracting programs. It's possible to compile an entire Java
application into a single executable class file that contains compressed and/or encrypted class file data, along with an integral ClassLoader; when the program is run, it unpacks itself entirely in memory --
no need to install first.
- Dynamic generation. They sky's the limit here. You can generate classes that refer to other classes that haven't been generated yet -- create entire classes on the fly and bring them into the JVM without missing a beat.