The Spring solution is the only working if run from an executable jar. Reflections requires Guava. I'll post a solution I've found in this duplicate question: How to get all classes names in a package? real life practical examples of reflection in java. extends Object>> classes = reflections.getSubTypesOf(Object.class); 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. Very good! If you try to read classes from file system, you may get errors when you package your application as JAR or other. if you're using it in any kind of production code, see my comment to his answer for a workaround. If it is not a class file but is a directory, we simply iterate into it and do the same thing. Note: In version 14, the API is still marked as @Beta, so beware in production code. Nov 2015 an Apache Commons problem discovered that leads to Remote Command Execution just by having Apache Commons in the classpath of an app deployed on Jboss/Weblogic [. reflections简单好用,性能也不错,很快就完成了我想要的功能,就是这么优秀! 简介. :), https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/20358207#20358207, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/20476490#20476490, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/44817646#44817646, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/32828953#32828953, It is not very useful because of: it is need, I can't make it working with a package of an external jar, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/2373928#2373928, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/34292253#34292253, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/44202417#44202417, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/25572809#25572809, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/46575084#46575084, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/56332568#56332568, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/58911838#58911838, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/61525596#61525596, https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/520335#520335. Note: There is a new version for this artifact. Note that this solution will not work as by default getSubTypesOf does not return subtypes of Object. Even if you specifically include java.lang and java.util as packages, neither reflections.getAllTypes() nor reflections.getSubTypesOf(Object.class) will give you the specialized Exception/LIst. However, when running Maven no classes where found with Aleksander Blomskøld's solution. How to get all classes names in a package? Provided you are not using any dynamic class loaders you can search the classpath and for each entry search the directory or JAR file. @Christian Good point, I have not noticed! Along with this, we will understand the sample code using reflection in Java and Java reflection class. So there's just no way to get all your classes with this library, is there? Good solution, but it seems to be better if 'Class.forName(String className)' will be replaced with 'Class.forName(String className, boolean initialize, ClassLoader loader)' where 'initialize = false;' in order to not create class instances. Using Reflections you can query your metadata such as: get all subtypes of some type github.com/lukehutch/fast-classpath-scanner. The answer was written by sp00m; I've added some corrections to make it work: To use it just call the find method as sp00n mentioned in this example: This code scans a given package for all classes that are included in it. The following are Jave code examples for showing how to use getSubTypesOf() of the org.reflections.Reflections class. If this returns an empty list, initialize the Reflections object like this: Reflections reflections = new Reflections("your.package.here", new SubTypesScanner(false)); https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/9571146#9571146, I ran in to problems on my Mac with this code (related to native libraries), but using. It then creates a URLConnection and determines what type of URl we have. That way, you can load (and instantiate) all the classes you desire regardless of what package they are in. code.google.com/p/reflections/issues/detail?id=122, github.com/ronmamo/reflections/blob/master/COPYING.txt, http://snippets.dzone.com/posts/show/4831, https://dzone.com/articles/get-all-classes-within-package, dzone.com/articles/get-all-classes-within-package, code.google.com/p/guava-libraries/#Important_Warnings, widest possible array of classpath specification mechanisms. There might even be libraries that can help you do this. It is hard because you haven't full instrument for get class name. The java.lang.reflect package provides many classes to implement reflection java.Methods of the java.lang.Class class is used to gather the complete metadata of a particular class. The snippet is also available at https://dzone.com/articles/get-all-classes-within-package. Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime. for instance, given your class C extends B extends A, and both B and A are located in another package than C, when only the package of C is scanned - then querying for sub types of A returns nothing (transitive), but querying for sub types of B returns C (direct). You can rate examples to help us improve the quality of examples. Reflections library has over 2.5 million downloads per month from Maven Central, and is being used by thousands of projects and libraries. "Reflections" library has a tricky license: https://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection/13477227#13477227. do you know that you are adding potential attack surface for attackers? I just wrote a util class, it include test methods, you can have a check ~. With it you can easily achieve what you want. Anyway, I just tried changing the. I'm using the Reflections lib. After all resources have been parsed it (the main method) returns the ArrayList containig all classes in the given package, that the current ClassLoader knows about. I promised to come up with my leanings/thoughts on this specific topic, and so here in this post, I will discuss the same i.e. Thanks but looks like it's documented - although that's weird about the interface thing.