How can I use a library deployed as an uberjar?

472 views Asked by At

I'm trying to use the Reflections 0.9.8 library, which comes as an uber-jar. If I add its JAR ("reflections-0.9.8-uberjar") within my project's build path then Eclipse won't find any of its class types: Reflection, ClasspathHelper and so on.

If I extract the single "reflections-0.9.8.jar" (not the uber one) and add it to the build path then at compile time everything works fine, but when I execute my project I get a NoClassDefFoundError (which I'm pretty sure it's caused by the lack of dependencies).

I've also tried to add every single .jar within the uber-jar, but I'll still get the NoClassDefFoundError.

What do I have to do to use this library distributed as an uberjar?

EDIT: In case it could help, this is the method in which I'd like to call Reflections (it's a snippet I took from Can you find all classes in a package using reflection?):

private static Set<Class<?>> getAllClassesInPackage(String pckg){
    List<ClassLoader> classLoadersList = new LinkedList<ClassLoader>();
    classLoadersList.add(ClasspathHelper.contextClassLoader());
    classLoadersList.add(ClasspathHelper.staticClassLoader());

    Reflections reflections = new Reflections(new ConfigurationBuilder()
        .setScanners(new SubTypesScanner(false), new ResourcesScanner())
        .setUrls(ClasspathHelper.forClassLoader(classLoadersList.toArray(new ClassLoader[0])))
        .filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix(pckg))));

    return reflections.getSubTypesOf(Object.class);

}
1

There are 1 answers

0
Martín C On

I had the same problem as you, the message NoClassDefFoundError. Searching for a while I have discovered that having jars inside other jars like the ones that exist inside the reflections-0.9.8.jar produce that problem. Nevertheless when I extract it as you did I see the same problem again. I've deduced that this can be due to the compiling version of the Reflection package (compiled in version 1.5) and that my project is compiled on 1.6.

I've decided to give up using the Reflections package and posted a ad-hoc solution to retrieve the classes in a package:

Can you find all classes in a package using reflection?

I hope it helps.