Java – Reflections – Java 8 – invalid constant type


I have a problem with Reflections library.
I am trying to load dynamically all classes which implement specific interface.
Everything works fine (all classes are loaded) as long as I do not use lambda expression in these classes (java 8).
I tried upgrade lib version but effect was the same ( invalid constant type: 18).

Dependency and build in pom.xml



without exclusion is the same effect.


    URL jarUrl = jarFile.toURI().toURL();
    URLClassLoader child = new URLClassLoader(new URL[]{jarUrl}, this.getClass().getClassLoader());
    ConfigurationBuilder builder = new ConfigurationBuilder()
            .setScanners(new SubTypesScanner());
    Reflections r = new Reflections(builder);
    return r.getSubTypesOf(cls);

How can I load classes with lambda expression?

P.S Sorry for English 🙂

Best Solution

If you look at this table, you’ll see that “constant type: 18” refers to the CONSTANT_InvokeDynamic attribute whose tag value is 18.

So you are using a library which has a class parser which is not Java 8 compatible. Actually, this class parser even isn’t Java 7 compatible as this constant value is specified since Java 7. It just got away with that as ordinary Java code doesn’t use this feature in Java 7. But when interacting with code produced by different programming languages for the JVM, it could even fail with Java 7.

There’s an item in the bug tracker of Reflections describing your problem. At the bottom, you will find the notice:

With this fix: javassist got support for this constant. So with 3.18.2-GA this error doesn't occur.