Java Spike: ClassLoader
Motivation
The ability to write custom ClassLoaders is one of the most powerful features available in a Java environment. If you can get your bytecode into an array of bytes then you can get that bytecode loaded into the JVM as though it were deployed as an ordinary class.
Amongst other things you can use this to:
-
write code that loads classes dynamically from a database
-
write a system that can directly load classes stored in encrypted files, or encoded in obscure archive formats
-
use a classloader in combination with a bytecode compiler to produce new classes on the fly
Features
If you run ClassLoaderSpike.java, an instance of the custom classloader in DemonstrationLoader.java will be created. An attempt will then be made to load a class called "TestClass" from this DemonstrationLoader.
The DemonstrationLoader will open a file called "TestClass" from the subdirectory "classees". Note the absence of the .class extension that the default classloader uses) from the subdirectory classes.