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

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

问题描述

NoClassDefFoundError ClassNotFoundException 之间有什么区别?

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

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. 我们正在使用的新罐子缺少运行时类路径

  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


当应用程序尝试加载
时抛出class通过其字符串
名称使用:

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


  • forName 方法class Class

  • findSystemClass 方法> ClassLoader 。

  • ClassLoader loadClass 方法$ C>。

  • 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虚拟机,则抛出
a ClassLoader 实例尝试在类的定义中加载
(作为普通方法调用的
部分或作为$ b的一部分$ b使用新的
表达式创建一个新实例,并且找不到
类的定义。

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 ,但在运行时,所需的找不到文件。这可能是在JAR文件的分发或生成中可能发生的事情,其中​​不包括所有必需的文件。

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.

两者之间的区别在于,一个是错误,另一个是异常。使用 NoClassDefFoundError 是一个错误,它来自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天全站免登陆