0

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);
    }
  • No exceptions thrown? What is the OUTPUT you get when you run this code? – GhostCat Feb 25 '22 at 07:43
  • I just updated the code(accidentally deleted "int count=0" on this post), I got an output of many class names and the number of the classes(34651), but the names of those two classes that I want were not included in the result. – Scarlett H Feb 25 '22 at 07:50
  • Dont provide such information on comments. Make sure all relevant information is in the question. And note: you define a URLClassloader and provide URLs. That doesn't LOAD the classes. You might have to tell the classloader to actually load the classes before you can fetch information about them? – GhostCat Feb 25 '22 at 07:54
  • `URLClassLoader`'s URLs should be a directory or a `.jar` file; not direct references to individual `.class` files. – Joe Feb 25 '22 at 08:31

1 Answers1

0

Using jar files instead of .class files fixed this!

 File file1 = new File("C:\\target.jar");
 URL url1 = file1.toURI().toURL();