How to build a list of classes annotated with my custom annotation?

Usually this is done using the process called classpath scanning. In general class loaders do not allow for scanning through all the classes on the classpath. But usually the only used class loader is UrlClassLoader from which we can retrieve the list of directories and jar files (see getURLs) and open them one by one to list available classes.

This approach is implemented by libraries like Scannotation and Reflections.

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.

The above mechanism is implemented in ClassIndex library.

Using classpath scanning is usually two orders of magnitude slower than compile-time indexing. See this benchmark.


classindex is a compile-time annotation scanning library, implemented using an annotation processor.


I know this is an old question, but I ran across it in my own search for classpath scanning and found another good answer, so I'm adding it here.

Google Guava has a ClassPath object that provides "best effort" classpath scanning (which is all any classpath scanning utility offers, really). Since Guava is a widely-adopted, carefully-maintained utility library, this is a great option for projects that either (a) are already using Guava, or (b) need a stable library they can rely on for classpath scanning.


You should take a look at Scannotation.