Let's say I have three classes, example.ClassA, example.ClassB, and example.ClassLoader. ClassA prints out HelloWorld and ClassB imports example.ClassA and invokes its main() method. If I do this:
java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassA
It works and uses my class loader. However, if I do this:
java -cp Example.jar -Djava.system.class.loader=example.ClassLoader example.ClassB
ClassB uses my class loader, but ClassA (which was imported by ClassB) is loaded using a default class loader.
Is there any way to force Java to always use my class loader (unless another class loader is specified explicitly)?
EDIT: Thanks to Paŭlo Ebermann's answer below, I figured the problem is that I'm calling the parent class loader (URLClassLoader) to load the classes that I don't need to touch, and those loaded classes set that as it's context class loader, so classes imported from it uses the parent class loader of my custom loader. (confusing, sorry) Now I can get it to work by manually reading in EVERY class, however it seems redundant as I've copied URLClassLoader's code directly. Is there a way to tell the parent class loader to find and define the class, BUT set the Class's context class loader to your custom one?