java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别(转)

 

ClassNotFoundException

ClassNotFoundException这个错误,比较常见也好理解。

  原因:就是找不到指定的class。

  常见的场景就是:

  1 调用class的forName方法时,找不到指定的类

  2 ClassLoader 中的 findSystemClass() 方法时,找不到指定的类

  3 ClassLoader 中的 loadClass() 方法时,找不到指定的类

java.lang.Class.java:

    /**
     * Returns the <code>Class</code> object associated with the class or
     * interface with the given string name.  Invoking this method is
     * equivalent to:
     *
     * <blockquote><pre>
     *  Class.forName(className, true, currentLoader)
     * </pre></blockquote>
     *
     * where <code>currentLoader</code> denotes the defining class loader of
     * the current class.
     *
     * <p> For example, the following code fragment returns the
     * runtime <code>Class</code> descriptor for the class named
     * <code>java.lang.Thread</code>:
     *
     * <blockquote><pre>
     *   Class&nbsp;t&nbsp;= Class.forName("java.lang.Thread")
     * </pre></blockquote>
     * <p>
     * A call to <tt>forName("X")</tt> causes the class named
     * <tt>X</tt> to be initialized.
     *
     * @param      className   the fully qualified name of the desired class.
     * @return     the <code>Class</code> object for the class with the
     *             specified name.
     * @exception LinkageError if the linkage fails
     * @exception ExceptionInInitializerError if the initialization provoked
     *            by this method fails
     * @exception ClassNotFoundException if the class cannot be located
     */
    public static Class<?> forName(String className)
                throws ClassNotFoundException {
        return forName0(className, true, ClassLoader.getCallerClassLoader());
    }

 

    /** Called after security checks have been made. */
    private static native Class forName0(String name, boolean initialize,
                        ClassLoader loader)
    throws ClassNotFoundException;

 

NoClassDefFoundError

这个就比较奇葩了,查找其他的资料是说,通过了编译,但是使用的时候,比如new的时候会出错。

  通过查找资料,搜集到如下的场景:

  1 类依赖的class或者jar不存在

  2 类文件存在,但是存在不同的域中

  3 大小写问题,javac编译的时候是无视大小的,很有可能你编译出来的class文件就与想要的不一样!这个没有做验证。

 

 

 

http://www.cnblogs.com/xing901022/p/4185514.html

 

时间: 2024-09-07 09:19:47

java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别(转)的相关文章

java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别

以前一直没有注意过这个问题,前两天机缘巧合上网查了一下,然后自己测试验证了一下.虽然网上说法很多,但是关于NoClassDefFoundError并没有给出一个样例,所以一直无法理解,索性自己验证了一下,收获还不少. ClassNotFoundException ClassNotFoundException这个错误,比较常见也好理解. 原因:就是找不到指定的class. 常见的场景就是: 1 调用class的forName方法时,找不到指定的类 2 ClassLoader 中的 findSyst

java.lang.ClassNotFoundException: javax.servlet.Filter

java.lang.ClassNotFoundException: javax.servlet.Filter:有两个原因:(1)在maven中的作用域,不能是provided,需要是compile就是默认的scope(2)spring-boot-starter-tomcat的版本bug,换个新的或旧的版本试试   I Started a new project with Spring Boot 1.2.3. I'm getting error java.lang.NoClassDefFoundE

Caused by: java.lang.ClassNotFoundException: org.apache.oro.text.regex.Malformed

问题描述 215 UNIX Type: L8Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/oro/text/regex/MalformedPatternException at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createUnixFTPEntryParser(DefaultFTPFileEnt

java web-javaweb运行出java.lang.ClassNotFoundException异常

问题描述 javaweb运行出java.lang.ClassNotFoundException异常 严重: Allocate exception for servlet Web.SearchK java.lang.ClassNotFoundException: Search.searchkey at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333) at org.

java.lang.ClassNotFoundException: org.hibernate.annotations.Entity

问题描述 最近要把hibernate从3.2升级到3.67.更换JAR包之后,报以下错误,但那个entity在hibernate3.jar包里面是有.先谢谢大家!gframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:282)atorg.springframework.web.context.ContextLoader.initWebApplicationContext(Context

maven spring3 hibernate4 Caused by: java.lang.ClassNotFoundException: org.hibe

问题描述 org.hibernate.engine.SessionFactoryImplementor三月13,201511:43:07上午org.apache.catalina.core.StandardContextlistenerStart严重:Exceptionsendingcontextinitializedeventtolistenerinstanceofclassorg.springframework.web.context.ContextLoaderListenerorg.spr

eclipse 使用Hibernate出错 java.lang.ClassNotFoundException

问题描述 eclipse使用Hibernate出错java.lang.ClassNotFoundException调试了一天了,各位帮帮忙啊出错信息如下:java.lang.NoClassDefFoundError:org/dom4j/DocumentExceptionorg.util.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:57)org.util.HibernateSessionFac

java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcessingException 异常解决方案

问题: 在SpringMVC中使用Jackson实现json输出时配置如下: <!-- 输出对象转JSON支持 --> <bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list>

java.lang.ClassNotFoundException: org.springframework.context.event.GenericApplicationListener ----good

  Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener From the above error, application is unable to find the class defined from the jar dependencies. GenericApplicationListener is added from spring