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

in Java
Java Tutorials
1 Answers to this question

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 Certification Training page for more information, where you can find the       and    Java Interview Questions FAQ's and answers    as well.

For more updates on the latest courses stay tuned to HKR Trainings.

To Top