Search code examples
javapackageclasspathguava

Google Guava getTopLevelClasses returns empty set


I've been searching around all over the internet to no avail. I am attempting to use Guava to get all the classes in a package of mine, but it is not behaving as intended. It always returns an empty set, making it impossible to do anything with the given results. Could there be a problem with System Variables, or some other road-block?

Here is some of my code.

        String packageName = "me.travja.package";
        ImmutableSet<ClassPath.ClassInfo> root = null;
        try {
            System.out.println(ClassPath.from(getClass().getClassLoader()));
            root = ClassPath.from(getClass().getClassLoader()).getTopLevelClasses();//.getTopLevelClassesRecursive(packageName);
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (ClassPath.ClassInfo info : root) {


            System.out.println(info.getPackageName() + " -- " + info.getSimpleName());
        }

It never hits the last sout because it's empty, but the one that prints the classpath prints 'com.google.common.reflect.ClassPath@33571c14' which isn't super useful. But to my knowledge, shouldn't that resemble more of my application's directory?

Thank you for your help with this. It's been bugging me for too long.

EDIT: I did some digging around. It seems that it works as intended if my file path doesn't contain a Space. I read a little that this used to be a problem with Guava in older versions, but I even tried using Maven and shading the latest version of Guava. Is there any way to fix this, or do I just have to be cautious that my file path never has a space in it?


Solution

  • After doing some more digging, one of the other dependencies that I was using had shaded an older version of Guava and that is what my code was using. As a result, it was broken. I used a decompiler so I could manually shade the ClassPath class from a newer Guava into my own code, and imported that. Works flawlessly now.