Any suggestions as to how to approach how I would find out a list of package names that exist on the current classpath?
This needs to be done programmatically at run time by one of the classes loaded (and executing) on the classpath (i.e. inside out, not outside in).
More details:
One approach I considered was to use reflection on each of the classes loaded thus far by the class loader, and extract the package names from them. However, my application already runs into thousands of classes, so I need a more efficient approach.
Another thing I considered was something analogous to finding out what JAR files were in the class path, and then do the directory listing parallel for each JAR. However, I don't know if this is possible from within the application/ how to do it.
Bonus points
Bonus points for anyone who suggests a way that can filter by top-level packages. E.g. show all packages that come under com.xyz
==> com.xyz.*
, com.xyz.*.*
Thanks!
Give http://code.google.com/p/reflections/ a shot.
If you do need to mount and scan jar files commons-vfs has this built in. That might make things a little easier if you have to go that route.
EDIT #1: You can get the classpath like so (from the example here):
From there you can look at the local file system classes, jars, etc.
EDIT #2: Here is a solution with VFS:
This code will give you all classpath-entries (including the jre libraries):
ClassPath
is part of apache bcel and can be found as an internal (but usable) class in the jre (com.sun.org.apache.bcel.internal.util
). AFAIK, the class is "internal" to avoid conflicts when a project requires the "real" bcel library.You may want to filter the libraries of the JRE (they're included as this is the real classpath)
The next step would be looking at each (JarFile-)entry, visit all subdirectories (recursivly) and if (and only if) a directory contains at least one class file, the name of this directory (relative to the classpath entry) can be converted to a package name (example:
META_INF
is a directory in all jar files but not a package name...)