No, this is not possible in an absolutely reliable form; however, in many cases this can be practical.
Due to the flexibility of Java class loaders, the classes defined in a particular package may not be known at run time (for example, consider a special class loader that defines classes on the fly, possibly downloading them from the network or compiling them ad-hoc). Thus, there is no standard Java API that allows you to list classes in a package.
In practice, however, you could do some tricks by searching for class paths for all classes and JAR files, creating a collection of fully qualified class names and doing the search as you wish. This strategy will work fine if you are sure that no active class loader will behave as described in the previous paragraph. For example (Java pseudo code):
Map<String, Set<String>> classesInPackage = new HashMap(); for (String entry : eachClasspathEntry()) { if (isClassFile(entry)) { String packageName = getPackageName(entry); if (!classesInPackage.containsKey(packageName)) { classesInPackage.put(packageName, new HashSet<String>()); } classesInPackage.get(packageName).add(getClassName(entry)); } else if (isJarOrZipFile(entry)) {
maerics
source share