public static List<String> getClassPaths(String packageName) {
List<String> classPaths = new ArrayList<>();
String packagePath = packageName.replace(".", "/");
File packageDir = new File(Thread.currentThread().getContextClassLoader().getResource(packagePath).getPath());
File[] files = packageDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
classPaths.addAll(getClassPaths(packageName + "." + file.getName()));
} else if (file.isFile()) {
String name = packageName + "." + file.getName();
classPaths.add(name.substring(0, name.length()-6));
}
}
}
return classPaths;
}
List<String> classPaths = getClassPaths("com.interfaces.dto");
返回值为 com.interfaces.dto.MyClass 这种全类名格式的字符串