zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    简单的理解原型链
    react->Context笔记
    工作上git指令小结
    vue 绑定事件如何传递参数的同时拿到事件对象
    vsCode卸载后重新安装,以前的插件有没有效果的解决方法
    mongo 分组 aggregation
    Redisson分布式锁原理
    Virtual server server already has a web module live-mix-1.0.2-t230 loaded at / therefore web module
    二进制中 1 的个数
    替换空格
  • 原文地址:https://www.cnblogs.com/softidea/p/4211223.html
Copyright © 2011-2022 走看看