如何在java中使用ClassPath::getAllClasses(Guava lib)?
我正在尝试加载我在此处定义的类,例如 DemoApplication.class 和 Test.class。我使用的是 guava 31.0.1-jre,并使用了 ClassPath.from().getAllClasses(),但它似乎只获取运行时类,而我想要的这两个类没有被加载。 我不确定我正在做的事情是否完全错误。
public static void main(String[] args) throws ClassNotFoundException, IOException {
File file = new File("C:\\DemoApplication.class");
URL url = file.toURI().toURL();
File file1 = new File("C:\\Test.class");
URL url1 = file1.toURI().toURL();
int count=0;
URLClassLoader classLoader = new URLClassLoader(new URL[]{url,url1});
for (ClassPath.ClassInfo classInfo : ClassPath.from(classLoader).getAllClasses()) {
if (classInfo.getName().contains("DemoApplication")){
// I didn't get any output here
System.out.println(classInfo.getName());
}
}
System.out.println(count);
}
I'm trying to load classes that I defined here such as DemoApplication.class and Test.class. I'm using guava 31.0.1-jre, and used ClassPath.from().getAllClasses(), but it seems to only get runtime classes and these two classes I want are not being loaded.
I'm not sure if what I'm doing is totally in the wrong way.
public static void main(String[] args) throws ClassNotFoundException, IOException {
File file = new File("C:\\DemoApplication.class");
URL url = file.toURI().toURL();
File file1 = new File("C:\\Test.class");
URL url1 = file1.toURI().toURL();
int count=0;
URLClassLoader classLoader = new URLClassLoader(new URL[]{url,url1});
for (ClassPath.ClassInfo classInfo : ClassPath.from(classLoader).getAllClasses()) {
if (classInfo.getName().contains("DemoApplication")){
// I didn't get any output here
System.out.println(classInfo.getName());
}
}
System.out.println(count);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 jar 文件而不是 .class 文件解决了这个问题!
Using jar files instead of .class files fixed this!