I would stick with the Class.forName method for this.
You can store class and package names in Collection or Set and scroll through these elements. When you get a ClassNotFoundException , you just continue the search. If you don't get the exception, you exit the loop with break , since you found the class you were looking for.
The reason for Class.forName is that it loads the class for you if it has not already been loaded by the virtual machine. This is a pretty powerful side effect.
This basically eliminates the need to dig up all CLASSPATH and look for class files to load into the virtual machine and solve problems such as the class is already loaded or not using the virtual machine.
EDIT:
Jacob Jenkov has written some really great Java articles / tutorials. I found them extremely useful when working with reflection, class loaders, and concurrency (some of the βmost complexβ aspects of Java).
Here's a good article on the Java class loader if you still decide not to use Class.forName .
source share