I have a problem, I wish to use reflection to generate instances of one of a set of classes at runtime. However, I have hit a snag. I want to get all of the classes involved to register themselves so the appropriate class can be chosen from a GUI. I can do this using a static code block in each file which provides a tidy OO solution. However, the java class loader specifically loads classes when they are required, hence if a needed class has not yet been used, it is not registered.
Short of directly providing a static list of names, or running through the underlying class/java files (which would break when packaged into a jar anyway), is there any way to force classes of certain packages to be loaded?
Basically, I want the ability to add new classes, from a specified superclass, without having to change/add any other code.
To clarify, your problem isn’t about “dynamic class loading in Java”, it’s about dynamic class enumeration — you know how to load classes, you just don’t know what classes you want.
A quick Google came up with this page: http://forums.sun.com/thread.jspa?threadID=341935&start=0&tstart=0
Taken from that page, here’s some sample code that ought to work: