0

Using the following code I get a NoClassDefFoundError caused by ClassNotFoundException:

File file = new File( "C:\\prototype.core.bl.xmodelval.xmodel-1.0.0.jar" );
URL url = file.toURI().toURL();
URL[] urls = new URL[] { url };
ClassLoader cl = new URLClassLoader( urls );
Class cls = cl.loadClass("de.zeb.control.prototype.core.bl.xmodelval.xmodel.entity.EntityNameLengthXModelValidationRule" );

The jar and the class in the jar are definitely available. The following exception is thrown by cl.loadClass. The full exception stack trace follows:

Exception in thread "main" java.lang.NoClassDefFoundError: de/zeb/control/prototype/core/bl/xmodelval/XModelValidationRule
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at de.zeb.control.prototype.core.bl.xmodelval.jarwelfare.loader.XModelJarLoader.load(XModelJarLoader.java:32)
    at de.zeb.control.prototype.core.bl.xmodelval.jarwelfare.supplier.Runner.main(Runner.java:29)
Caused by: java.lang.ClassNotFoundException: de.zeb.control.prototype.core.bl.xmodelval.XModelValidationRule
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 24 more

It seems strange for me that the full class name of the ClassNotFoundException doesn't match the name of the cl.loadClass.

Thank you for your help.

leogl
  • 11
  • 2
  • It's some sort of convoluted problem due to something like loading the class in the wrong class loader. Hard to say what without seeing the entire class loader layout, though. – Hot Licks Apr 17 '14 at 13:02
  • It appears that, basically, while loading the XModelValidationRule class, several other classes needed to be loaded to verify that class. One of those classes in turn references XModelValidationRule, and when it went looking for it, the class loader for XModelValidationRule was not in the class loader chain, so XModelValidationRule was not found. – Hot Licks Apr 17 '14 at 13:06
  • Thank you, it works! The problem was, like you said, that the XModelValidationRule wasn't on the class path. Adding the dependency containing the class solved the problem. – leogl Apr 17 '14 at 13:40

1 Answers1

3

You might need to provide parent classloader, because otherwise the classloader you define won't be able to access any classes already present in your application. E.g.:

 ClassLoader cl = new URLClassLoader( urls , getClass().getClassLoader());
Vladimír Schäfer
  • 15,375
  • 2
  • 51
  • 71