vendredi 29 juin 2018

NoClassDefFoundError loading class with URLClassLoader

We are trying to support plugins for our product and I am using reflection to load a class from a jar.

I create a URL ClassLoader and add all the dependency jars as URLs.

I use that ClassLaoder to load the primary class of the plugin.

I get an instance of the class and a Method reference (a run() method).

When I invoke the method I get this exception:

java.lang.NoClassDefFoundError: Could not initialize class org.apache.logging.log4j.util.PropertiesUtil

This class is in log4j-api-2.10.0.jar, which is included in the classpath of the ClassLoader.

I can see the URL in the classloader in debug so I know its properly referenced.

Sometimes I get a different error, saying that it can't initialize one of my classes but that class is nothing special, no statics or anything.

The primary class that's being loaded by reflection is calling a second class that uses Log4j 2.

When the primary class run() method tries to instantiate the 2nd class that's where this problem occurs.

There is nothing static in either class other than the Logger.

In the debugger I can see from the Method ref that the class was indeed loaded with the URL ClassLoader we created.

This code has been working for a decade and the only thing we've changed is the upgrade to Log4j 2 in the plugin. The class doing the reflective plugin load is just a normal class in the classpath of the application.

Any help would be greatly appreciated.





Aucun commentaire:

Enregistrer un commentaire