I need to read classes contained in a Java package. Those classes are in classpath. I need to do this task from a Java program directly. Do you know a simple way to do?
List<Class> classes = readClassesFrom("my.package")
I need to read classes contained in a Java package. Those classes are in classpath. I need to do this task from a Java program directly. Do you know a simple way to do?
List<Class> classes = readClassesFrom("my.package")
Spring has implemented an excellent classpath search function in the
PathMatchingResourcePatternResolver
. If you use theclasspath*
: prefix, you can find all the resources, including classes in a given hierarchy, and even filter them if you want. Then you can use the children ofAbstractTypeHierarchyTraversingFilter
,AnnotationTypeFilter
andAssignableTypeFilter
to filter those resources either on class level annotations or on interfaces they implement.use dependency maven:
then use this code :
Brent - the reason the association is one way has to do with the fact that any class on any component of your CLASSPATH can declare itself in any package (except for java/javax). Thus there just is no mapping of ALL the classes in a given "package" because nobody knows nor can know. You could update a jar file tomorrow and remove or add classes. It's like trying to get a list of all people named John/Jon/Johan in all the countries of the world - none of us is omniscient therefore none of us will ever have the correct answer.
I happen to have implemented it, and it works in most cases. Since it is long, I put it in a file here.
The idea is to find the location of the class source file which is available in most cases (a known exception are JVM class files -- as far as I've tested). If the code is in a directory, scan through all files and only spot class files. If the code is in a JAR file, scan all entries.
This method can only be used when:
You have a class that is in the same package you want to discover, This class is called a SeedClass. For example, if you want to list all classes in 'java.io', the seed class may be
java.io.File
.Your classes are in a directory or in a JAR file it has source file information (not source code file, but just source file). As far as I've tried, it work almost 100% except the JVM class (those classes come with the JVM).
Your program must have permission to access ProtectionDomain of those classes. If your program is loaded locally, there should be no problem.
I've tested the program only for my regular usage, so it may still have problem.
I hope this helps.
Java 1.6.0_24:
This solution was tested within the EJB environment.
Scannotation and Reflections use class path scanning approach:
Another approach is to use Java Pluggable Annotation Processing API to write annotation processor which will collect all annotated classes at compile time and build the index file for runtime use. This mechanism is implemented in ClassIndex library: