问题
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.
回答1:
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());
来源:https://stackoverflow.com/questions/23132980/loadclass-throws-noclassdeffounderror-caused-by-classnotfoundexception