As required by the project, I want to obtain all the classes under a certain package (including all the sub-packages of this package) and find them online. There is nothing available, even if you find something to write, the efficiency is low. Simply write it on your own, and exercise your skills in writing code. I would like to share with you the hope to help you ......
[Java]
Package com. itkt. mtravel. hotel. util;
Import java. io. File;
Import java. util. ArrayList;
Import java. util. List;
Public class PackageUtil {
Public static void main (String [] args ){
String packageName = "com. itkt. mtravel. hotel ";
List <String> classNames = getClassName (packageName );
For (String className: classNames ){
System. out. println (className );
}
}
Public static List <String> getClassName (String packageName ){
String filePath = ClassLoader. getSystemResource (""). getPath () + packageName. replace (".","\\");
List <String> fileNames = getClassName (filePath, null );
Return fileNames;
}
Private static List <String> getClassName (String filePath, List <String> className ){
List <String> myClassName = new ArrayList <String> ();
File file = new File (filePath );
File [] childFiles = file. listFiles ();
For (File childFile: childFiles ){
If (childFile. isDirectory ()){
MyClassName. addAll (getClassName (childFile. getPath (), myClassName ));
} Else {
String childFilePath = childFile. getPath ();
ChildFilePath = childFilePath. substring (childFilePath. indexOf ("\ classes") + 9, childFilePath. lastIndexOf ("."));
ChildFilePath = childFilePath. replace ("\\",".");
MyClassName. add (childFilePath );
}
}
Return myClassName;
}
}
Nothing is especially complicated. You can understand it basically. If the namespace of any package is input, all classes under the package can be returned. I feel pretty good, easy to use, general and scalable. As a matter of fact, after skilled basic skills, the rest is the idea of assembly.