NoClassDefFoundError 和 ClassNotFoundException 的原因和区别是什么? [英] What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?

查看:26
本文介绍了NoClassDefFoundError 和 ClassNotFoundException 的原因和区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NoClassDefFoundErrorClassNotFoundException 有什么区别?

是什么导致它们被抛出?如何解决?

What causes them to be thrown? How can they be resolved?

在修改现有代码以包含新的 jar 文件时,我经常遇到这些 throwable.对于通过 webstart 分发的 Java 应用程序,我在客户端和服务器端都使用了它们.

I often encounter these throwables when modifying existing code to include new jar files. I have hit them on both the client side and the server side for a java app distributed through webstart.

我遇到的可能原因:

  1. 未包含在 build.xml 中的用于代码客户端的包
  2. 我们正在使用的新 jar 缺少运行时类路径
  3. 版本与之前的 jar 冲突
  1. packages not included in build.xml for the client side of code
  2. runtime classpath missing for the new jars we are using
  3. version conflicts with previous jar

当我今天遇到这些时,我会采取跟踪错误的方法来使事情正常进行.我需要更多的清晰度和理解力.

When I encounter these today I take a trail-and-error approach to get things working. I need more clarity and understanding.

推荐答案

与Java API规范的区别如下.

The difference from the Java API Specifications is as follows.

对于 ClassNotFoundException:

当应用程序试图通过其字符串加载到一个类中名称使用:

Thrown when an application tries to load in a class through its string name using:

  • Class 类中的 forName 方法.
  • ClassLoader 类中的 findSystemClass 方法.
  • ClassLoader 类中的 loadClass 方法.
  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader.
  • The loadClass method in class ClassLoader.

但是没有定义类可以找到指定的名称.

but no definition for the class with the specified name could be found.

对于 NoClassDefFoundError:

如果 Java 虚拟机或ClassLoader 实例尝试加载在类的定义中(作为一部分正常方法调用或作为的一部分使用 new 创建一个新实例表达式)并且没有定义可以找到类.

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

搜索到的类定义当前执行时存在类已编译,但定义再也找不到了.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

所以,看起来NoClassDefFoundError发生在源代码编译成功时,但在运行时,没有找到所需的class文件.这可能会发生在 JAR 文件的分发或生产中,其中并未包含所有必需的 class 文件.

So, it appears that the NoClassDefFoundError occurs when the source was successfully compiled, but at runtime, the required class files were not found. This may be something that can happen in the distribution or production of JAR files, where not all the required class files were included.

至于ClassNotFoundException,看起来可能是因为在运行时试图对类进行反射调用,但程序试图调用的类并不存在.

As for ClassNotFoundException, it appears that it may stem from trying to make reflective calls to classes at runtime, but the classes the program is trying to call is does not exist.

两者的区别在于一个是Error,另一个是Exception.NoClassDefFoundError 是一个 Error,它是由于 Java 虚拟机在寻找它期望找到的类时遇到问题而引起的.由于未找到 class 文件,或者与编译时生成或遇到的文件不一样,预期在编译时工作的程序无法运行.这是一个非常严重的错误,因为程序无法由 JVM 启动.

The difference between the two is that one is an Error and the other is an Exception. With NoClassDefFoundError is an Error and it arises from the Java Virtual Machine having problems finding a class it expected to find. A program that was expected to work at compile-time can't run because of class files not being found, or is not the same as was produced or encountered at compile-time. This is a pretty critical error, as the program cannot be initiated by the JVM.

另一方面,ClassNotFoundException 是一个 Exception,所以它有点在意料之中,并且是可以恢复的.使用反射可能容易出错(因为有些人认为事情可能不会按预期进行.没有编译时检查是否存在所有必需的类,因此在运行时会出现查找所需类的任何问题.

On the other hand, the ClassNotFoundException is an Exception, so it is somewhat expected, and is something that is recoverable. Using reflection is can be error-prone (as there is some expectations that things may not go as expected. There is no compile-time check to see that all the required classes exist, so any problems with finding the desired classes will appear at runtime.

这篇关于NoClassDefFoundError 和 ClassNotFoundException 的原因和区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆