出由ClassNotFoundException引起的NoCla

出由ClassNotFoundException引起的NoCla

本文介绍了loadClass抛出由ClassNotFoundException引起的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码,我收到由ClassNotFoundException引起的NoClassDefFoundError:

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" );

jar和jar中的类肯定可用. cl.loadClass引发以下异常.完整的异常堆栈跟踪如下:

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

对于我来说,ClassNotFoundException的完整类名与cl.loadClass的名称不匹配似乎很奇怪.

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

谢谢您的帮助.

推荐答案

您可能需要提供父类加载器,因为否则,您定义的类加载器将无法访问应用程序中已经存在的任何类.例如:

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());

这篇关于loadClass抛出由ClassNotFoundException引起的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:57