javaannotationsnoclassdeffounderror

Can't get org.reflections.Reflections to work, NoClassDefFoundError


I'm not sure what I'm doing wrong with Reflections. So here's an example, I have a package testreflections.test, with the classes Main and TestClass inside it.

Main class:

public class Main {

    public static void main(String[] args) {
        Reflections reflections = new Reflections("testreflections.test");
        //Reflections reflections = new Reflections("testreflections.test.TestClass");//I also tried this
        Set<Method> resources =
            reflections.getMethodsAnnotatedWith(org.junit.Test.class);

        System.out.println(resources.size());

         for (Method set : resources) {
             System.out.println(set.toString());
         }
    }
}

TestClass:

public class TestClass {

    @Test
    public void testMethod() {
        System.out.println("This is a test");
    }
}

Now, I'm simply supposed to get get all the methods with the junit Test annotation (which would just be 1). This should add the one to the set, and print out the appropriate info for it. However, eclipse spits out this error:

Exception in thread "main" java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile
at org.reflections.adapters.JavassistAdapter.getOfCreateClassObject(JavassistAdapter.java:100)
at org.reflections.adapters.JavassistAdapter.getOfCreateClassObject(JavassistAdapter.java:24)
at org.reflections.scanners.AbstractScanner.scan(AbstractScanner.java:30)
at org.reflections.Reflections.scan(Reflections.java:238)
at org.reflections.Reflections.scan(Reflections.java:204)
at org.reflections.Reflections.<init>(Reflections.java:129)
at org.reflections.Reflections.<init>(Reflections.java:170)
at org.reflections.Reflections.<init>(Reflections.java:143)
at testreflections.test.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: javassist.bytecode.ClassFile
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 9 more

Solution

  • It turns out I just needed to add Javaassist to the classpath.