Skip to main content

How to find all the packages in a Project with a Prefix

Points To Remember

  • You to need to add the Reflections jar in the class path. 
  • You will not be able to load the packages other than the current package using Package.getPackages() since current class loader will not be able to reach them.

Program : Scan all packages with a Prefix

You can use Package.getPackages() approach to find all the packages that are in the current class loader. But you will not be able to get the packages that are not in the class path of the current class loader. So you need to include the jar "Reflections" to do this for you.
All the dependency to the project via pom.xml or you can add the jar in the class path of the project.

The following code will list all the packages in the project that start with the prefix in the project.

 public static Set<String> findAllPackagesStartingWith(String prefix) {
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false), new ResourcesScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(prefix))));
Set<Class<? extends Object>> classes = reflections.getSubTypesOf(Object.class);

Set<String> packageNameSet = new TreeSet<String>();
for (Class<?> classInstance : classes) {
String packageName = classInstance.getPackage().getName();
if (packageName.startsWith(prefix)) {
packageNameSet.add(packageName);
}
}
for(String t : packageNameSet){
System.out.println(":::"+t);
}
return packageNameSet;
}

If you want the packages in the project in the form of Package class you can do
public Set<Package> findAllPackagesStartingWith(String prefix) {
List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
classLoadersList.add(ClasspathHelper.contextClassLoader());
classLoadersList.add(ClasspathHelper.staticClassLoader());
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setScanners(new SubTypesScanner(false), new ResourcesScanner())
.setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(prefix))));
Set<Class<? extends Object>> classes = reflections.getSubTypesOf(Object.class);

Set<Package> packageNameSet = new TreeSet<Package>();
for (Class<?> classInstance : classes) {
if (classInstance.getPackage().getName().startsWith(prefix)) {
packageNameSet.add(classInstance.getPackage());
}
}

return packageNameSet;
}
When we run the above code in a demo java project and call the function as findAllPackagesStartingWith("java"); we will get a output like the following. The output may differ for you depending upon the packages in your project.
package = javassist
package = javassist.bytecode
package = javassist.bytecode.analysis
package = javassist.bytecode.annotation
package = javassist.bytecode.stackmap
package = javassist.compiler
package = javassist.compiler.ast
package = javassist.convert
package = javassist.expr
package = javassist.runtime
package = javassist.scopedpool
package = javassist.tools
package = javassist.tools.reflect
package = javassist.tools.rmi
package = javassist.tools.web
package = javassist.util
package = javassist.util.proxy
package = javax.annotation
package = javax.annotation.concurrent
package = javax.annotation.meta
package = javax.xml.parsers
package = javax.xml.transform
package = javax.xml.transform.dom
package = javax.xml.transform.sax
package = javax.xml.transform.stream

Comments