输入为包名,输出该包名下面所有类的字节码文件的绝对路径,也就是获取\target\classes\路径下的.class文件的绝对路径,代码参考如下
public class Demo01 {
public static void main(String[] args) {
// 输入包的路径
String packagePath = "com.xx.yy.test";
packagePath = packagePath.replace(".", "/");
ClassLoader classLoader = SpringDemo01.class.getClassLoader();
URL resource = classLoader.getResource(packagePath);
File file = new File(resource.getFile());
if (file.isDirectory()){
for (File f : file.listFiles()) {
String absolutePath = f.getAbsolutePath();
System.out.println(absolutePath); // 输出绝对路径
}
}
}
}