How can I programmatically get a list of all of the classes in a given package?

Berin Loritsch

This is not nicely supported by the Java API, but it is possible. Thanks to input from the guys at HotDispatch, I have the answer.

Basically, you have to use the file system to access the classes in a package. Packages map to the file system in the following manner:


File System:

You would use a FileNameFilter to choose only the files with the extention ".class". If your classes are neatly wrapped up in a jar file, then use the JarFile object to navigate within the jar file.

In order to make the solution more complete, you need to create a custom classloader that keeps track of what Package resides in each jar file. Using your custom classloader, you can add hooks to get the class lists as Class objects or String