使用以下代码,我得到由ClassNotFoundException引起的NoClassDefFoundError:
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" );
罐子和罐子中的类肯定可用。 cl.loadClass引发以下异常。完整的异常堆栈跟踪如下:
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
对我来说,奇怪的是ClassNotFoundException的完整类名与cl.loadClass的名称不匹配。
谢谢您的帮助。
最佳答案
您可能需要提供父类加载器,因为否则定义的类加载器将无法访问应用程序中已经存在的任何类。例如。:
ClassLoader cl = new URLClassLoader( urls , getClass().getClassLoader());
关于java - loadClass抛出由ClassNotFoundException引起的NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23132980/