All packages in java project

I want to print all classes in all packages of my Java project. For example, suppose a project contains

Package1.Class1 Package1.Class2 Package1.Class3 Package2.Class1 Package2.Class2 

I would like to print all of these packages and classes. Using the following code, I can list them, but only when I refer to Package.Class c = new Package.Class();

Here is the code I'm trying to use:

 Package[] pa = Package.getPackages(); for (int i = 0; i < pa.length; i++) { Package p = pa[i]; System.out.print("\"" + p.getName() + "\", "); } 

Any thoughts on how I can do this?

Thank you all

+4
source share
2 answers

There is no easy way because of the dynamic nature of class loaders.

However, you can look at the reflection library: http://code.google.com/p/reflections/

This will allow you to search for classes in the current class path.

With this library, you can try something like this:

  Reflections reflects = new Reflections("my.project.prefix"); Set<Class<? extends Object>> allClasses = reflects.getSubTypesOf(Object.class); 
+3
source

Go with reflections or analyze the path to the classes, dividing it into separate folders and / or jar files, going each of them and specifying packages. But this is more complicated than using the reflection library.

+2
source

All Articles