Provide a custom implementation of ClassLoader.
Override the findClass()
method of the classloader and apply the business logic for selecting the classes that you want to be loaded.
class CustomClassLoader extends ClassLoader {
public Class findClass(String name) {
if(shouldBeLoaded)
return defineClass(name, b, 0, b.length);
}
}
Setting this as the default class loader for loading (optional)
java -Djava.system.class.loader
=com.test.CustomClassLoader