Referring to the source enSO + small change, then one of the options can be done so (looking for all the classes in the specified package):
import java.io.File; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.List; public class ClassFinder { private static final char PKG_SEPARATOR = '.'; private static final char DIR_SEPARATOR = '/'; private static final String CLASS_FILE_SUFFIX = ".class"; private static final String BAD_PACKAGE_ERROR = "Unable to get resources from path '%s'. Are you sure the package '%s' exists?"; public static List<Class<?>> find(String scannedPackage, String beginStr) throws URISyntaxException { String scannedPath = scannedPackage.replace(PKG_SEPARATOR, DIR_SEPARATOR); URL scannedUrl = Thread.currentThread().getContextClassLoader().getResource(scannedPath); if (scannedUrl == null) { throw new IllegalArgumentException(String.format(BAD_PACKAGE_ERROR, scannedPath, scannedPackage)); } File scannedDir = new File(scannedUrl.toURI()); List<Class<?>> classes = new ArrayList<Class<?>>(); for (File file : scannedDir.listFiles()) { classes.addAll(find(file, scannedPackage, beginStr)); } return classes; } private static List<Class<?>> find(File file, String scannedPackage, String beginStr) { List<Class<?>> classes = new ArrayList<Class<?>>(); String resource = scannedPackage + PKG_SEPARATOR + file.getName(); if (file.isDirectory()) { for (File child : file.listFiles()) { classes.addAll(find(child, resource, beginStr)); } } else if (resource.endsWith(CLASS_FILE_SUFFIX)) { int endIndex = resource.length() - CLASS_FILE_SUFFIX.length(); String className = resource.substring(0, endIndex); try { Class foundedClass = Class.forName(className); String[] bits = foundedClass.getName().split("\\."); String lastOne = bits[bits.length-1]; if (lastOne.toLowerCase().substring(0, beginStr.length()).equals(beginStr.toLowerCase())) classes.add(foundedClass); } catch (ClassNotFoundException ignore) { } } return classes; } }
and psvm:
public static void main (String[] args) throws java.lang.Exception { // пример поиска ClassFinder.find("com.myproject.java", "Hello"); List<Class<?>> classes = ClassFinder.find("Название пакета с полным путем", "Первые буквы класса"); for (Class item : classes) { System.out.println(item.getName()); } }
Another option (also looking for all the classes in the specified package):
import java.io.File; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.List; public class ClassFinder2 { public static List<Class> find(String pkgname, String beginStr) { List<Class> classes = new ArrayList<Class>(); File directory = null; String relPath = pkgname.replace('.', '/'); URL resource = ClassLoader.getSystemClassLoader().getResource(relPath); if (resource == null) { throw new RuntimeException("No resource for " + relPath); } try { directory = new File(resource.toURI()); } catch (URISyntaxException e) { throw new RuntimeException(pkgname + " (" + resource + ") does not appear to be a valid URL / URI. Strange, since we got it from the system...", e); } catch (IllegalArgumentException e) { directory = null; } if (directory != null && directory.exists()) { // Get the list of the files contained in the package String[] files = directory.list(); for (int i = 0; i < files.length; i++) { // we are only interested in .class files if (files[i].endsWith(".class")) { // removes the .class extension String className = files[i].substring(0, files[i].length() - 6); if (className.toLowerCase().substring(0, beginStr.length()).equals(beginStr.toLowerCase())) { String fullClassName = pkgname + '.' + className; try { classes.add(Class.forName(fullClassName)); } catch (ClassNotFoundException e) { throw new RuntimeException("ClassNotFoundException loading " + fullClassName); } } } } } return classes; } }
psvm:
public static void main (String[] args) throws java.lang.Exception { // пример поиска ClassFinder.find("com.myproject.java", "Hello"); List<Class> classes = ClassFinder2.find("Название пакета с полным путем", "Первые буквы класса"); for (Class item : classes) { System.out.println(item.getName()); } }