How to find all the classes of a package in Java?


In my project I need to find all the classes of certain packages: what is the best way to do this?



Yes, it is possible to find classes of a package in Java. You can Use reflections to find the same. 

Below is the code that helps you in determining the classes of a package using reflections. 

private Class<?>[] scanForTasks(String packageStr) 


        Reflections reflections = new Reflections((new ConfigurationBuilder()).setScanners(new Scanner[]

{new SubTypesScanner(), new TypeAnnotationsScanner()}).setUrls(ClasspathHelper.forPackage(packageStr, new ClassLoader[0])).filterInputsBy((new FilterBuilder()).includePackage(packageStr)));

        Set classes = reflections.getTypesAnnotatedWith(Task.class);

        Class[] taskArray = (Class[])classes.toArray(new Class[classes.size()]);

        return taskArray;




If you want to unleash your potential in this competitive field, please visit the Java course page for more information, where you can find the Java tutorials and Java frequently asked interview questions and answers as well.


This topic has been locked/unapproved. No replies allowed

Login to participate in this discussion.

Leave a reply

Before proceeding, please check your email for a verification link. If you did not receive the email, click here to request another.