private static List<Class> getClass(String packageName) {
+ "/WebContent/WEB-INF/classes/" + packageName;
List<Class> fileNames = getClass(filePath, null);
return fileNames;
}
private static List<Class> getClass(String filePath, List<Class> className) {
List<Class> myClass = new ArrayList<Class>();
File file = new File(filePath);
File[] childFiles = file.listFiles();
for (File childFile : childFiles) {
if (childFile.isDirectory()) {
myClass.addAll(getClass(childFile.getPath(), myClass));
} else {
String childFilePath = childFile.getPath();
childFilePath = childFilePath.substring(childFilePath
.indexOf("\\classes") + 9, childFilePath
.lastIndexOf("."));
childFilePath = childFilePath.replace("\\", ".");
try {
myClass.add(Class.forName(childFilePath));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
return myClass;
}
File file = new File("");
// 为了打包后运行jar包可以得到地址
+ "/WebContent/WEB-INF/classes/" + packageName;
List<Class> fileNames = getClass(filePath, null);
return fileNames;
}
private static List<Class> getClass(String filePath, List<Class> className) {
List<Class> myClass = new ArrayList<Class>();
File file = new File(filePath);
File[] childFiles = file.listFiles();
for (File childFile : childFiles) {
if (childFile.isDirectory()) {
myClass.addAll(getClass(childFile.getPath(), myClass));
} else {
String childFilePath = childFile.getPath();
childFilePath = childFilePath.substring(childFilePath
.indexOf("\\classes") + 9, childFilePath
.lastIndexOf("."));
childFilePath = childFilePath.replace("\\", ".");
try {
myClass.add(Class.forName(childFilePath));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
return myClass;
}