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

  • 相关阅读:
    A. Dawid and Bags of Candies ( Codeforces Round #588 (Div. 2) )
    B. Ania and Minimizing (Codeforces Round #588 (Div. 2) )
    残缺的棋盘 (BFS)
    Max Sum (动态规划)
    高桥和低桥 (离散化 )
    White Sheet (矩形面积模板) (Codeforces Round #587 (Div. 3) )
    Catch That Cow (BFS luo搜 + 剪枝)
    Python笔记-字符串
    关于拖延症
    一些告诫
  • 原文地址:https://www.cnblogs.com/softidea/p/4211223.html
Copyright © 2011-2022 走看看