Archived

This topic is now archived and is closed to further replies.

NickB

Emumerate Java Classes in a Package

Recommended Posts

Does anyone know if there is a way that I can obtain a list/enumeration of classes that exist in a Java package (ie, for a plugin system say) ? (given that classes might be in a JAR or just as class files). Thanks !

Share this post


Link to post
Share on other sites
Nothing like that exists in the core packages that I''m aware of. It can be implemented by extending the java.lang.ClassLoader abstract class an implementing the desired functionality. You can scan the classpath or any directories for existing class files (jar''ed or otherwise) and store them all in a List, for example.

Check out the jutils project at java.net. There''s a Plugin loader in there that does something similar to what you''re after. It is actually only loads classes that implement a Plugin interface, but if you look at the code you will see how it scans every class it finds to look for Plugins. It sholdn''t be to hard to modify it to store a list of ever class in a specific package.

Share this post


Link to post
Share on other sites
You could perhaps make a system call to get a directory listing, parse that to find .class files and chop off the .class extensions, then use reflection to create the corresponding Class objects? o__O

Share this post


Link to post
Share on other sites
Okay, so there isn''t an easy way - thanks for your responses. Is there a way that I can relate a package back to either a path or a path + jar file, so that I can limit the area I search (I know what package the classes should be in & they''d be relative to an interface class...so if I can get its path details that''d be useful) ?

Share this post


Link to post
Share on other sites