Andrio
Andrio

Reputation: 2078

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

Upvotes: 2

Views: 12549

Answers (3)

ombits
ombits

Reputation: 11

please use

 --jars  /hydra-analytics/gobblin-dist/lib/reflections-0.9.10.jar,/hydra-analytics/gobblin-dist/lib/guava-retrying-2.0.0.jar,/hydra-analytics/gobblin-dist/lib/javassist-3.18.2-GA.jar, ... 

with command

Upvotes: 0

W&#252;rgspa&#223;
W&#252;rgspa&#223;

Reputation: 4820

I suggest you to resolve the dependencies of org.reflections.Reflections. The easiest way is to use maven as described here. If not, the required classpath is described here. Make sure to download all required jar files and include them in the classpath of your Eclipse project.

In my comment I missed the part that you only want to find annotations. With standard Java you could use the getDeclaredAnnotations() method of java.lang.reflect.Method. But it is not that simple to gather all methods.

Upvotes: 1

Andrio
Andrio

Reputation: 2078

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

Upvotes: 0

Related Questions