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 t = 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端