I'm trying to load a jar with javassist to make it available in the application's current classloader, and I'm getting a CannotCompileException
due to a NoClassDefFoundError
What I got so far is as below:
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(jarFile);
List<String> classes = listJarClasses(jarFile);
for (final String className : classes) {
CtClass ctClass = pool.get(className);
ctClass.toClass(getClass().getClassLoader(), getClass().getProtectionDomain());
}
The listJarClasses
method will just iterate the jar's entries and return the found class names.
I understand this method will indeed load classes to the current classloader, but so far I can't guarantee they will be created in a proper order.
For example, I'm iterating the classes and I got ClassA
to be added, but this class needs ClassB
to be compiled and ClassB
is yet to be added to the classloader by the loop, thus the NoClassDefFoundError
.
I also understand that there is a way to load classes through URLClassLoader
as shown here, but this will not solve the problem, since the class will not be available on the current classloader for future use.
Is there a way to make it simpler to add a jar to the classloader and avoid this?