Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)
Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package, it would seem like no.)

2 Answers

0 votes
by (46k points)

It's not possible due to the active nature of class loaders. Class loaders aren't needed to tell the VM which classes it can produce, instead, they are just returned requests for classes, and have to return a class or yield an exception.

Still, if you write your class loaders or review the classpaths and it's clashing, it's possible to find this data. This will be through filesystem operations though, and not reflection. There might even be libraries that can assist you with this.

If there are classes that get created or delivered remotely, you will not be able to see those classes.

The standard method is rather somewhere register the classes you need access to in a file or reference them in a distinct class. Or just use a code when it comes to naming.

Addendum: The Reflections Library will allow you to see classes in the current classpath. It can be done to get all the classes in a package:

 Reflections reflections = new Reflections("my.project.prefix");

 Set<Class<? extends Object>> allClasses = 

     reflections.getSubTypesOf(Object.class);

Click here to take a look at open source Reflection Library.

0 votes
by (140 points)

Yes, you can do it with Burningwave Core: take a look to this example

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...