英文:
What is Difference between ClassNotFoundException vs NoClassDefFoundError vs Could not find or load main class XYZ?
问题
我探索了多个网站,但实际上无法理解它们之间的区别。我想知道这三者之间的确切区别。
英文:
I explored multiple sites but could not actually understand difference between them.I would like to know the exact difference between three.
答案1
得分: 4
A NoClassDefFoundError
is thrown if a classfile references a class that couldn't be found at runtime but was available at compiletime.
(来源:https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/NoClassDefFoundError.html)
当应用程序尝试通过以下方式之一使用其字符串名称加载类时,会抛出ClassNotFoundException
:
- 在
Class
类中的forName
方法。 - 在
ClassLoader
类中的findSystemClass
方法。 - 在
ClassLoader
类中的loadClass
方法。
(来源:https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/ClassNotFoundException.html)
错误消息Couldn't find or load main class XYZ
意味着许多情况:
- 您指定了错误的类(拼写错误)。
- 类文件中指定的类(不)在包中。(例如,
java c
,但类在包a.b中,所以命令应该是java a.b.c
)。
更多信息/原因:https://stackoverflow.com/a/18093929/13912132
英文:
A NoClassDefFoundError
is thrown,if a classfile references a class, that couldn't be found at runtime, but was available at compiletime.
(Source: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/NoClassDefFoundError.html)
A ClassNotFoundException
is thrown when an application tries to load in a class through its string name using:
- The forName method in class Class.
- The findSystemClass method in class ClassLoader .
- The loadClass method in class ClassLoader.
(Source: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/ClassNotFoundException.html)
The error message Couldn't find or load main class XYZ
, means a lot of things:
- You specified the wrong class (Typo)
- The class in the classfile specified is (not) in a package. (Like
java c
, but the class is in the package a.b, so the command should bejava a.b.c
)
More information/causes: https://stackoverflow.com/a/18093929/13912132
答案2
得分: 2
NoClassDefFoundError
- 是在运行时抛出的运行时错误,当类的字节码(即相应的.class
文件)在运行时无法找到时会抛出。
- 想象一下,您已经编译了类
A.java
和B.java
,并且A
正在使用B
;然而,稍后您删除了已编译的B.class
文件。因此,编译顺利进行,但后来,类定义的实际字节码已被删除,因为您删除了B.class
。现在,如果运行使用B
的A
,您将最终遇到java.lang.NoClassDefFoundError
。
ClassNotFoundException
- 是一个已检查异常,当您的应用程序尝试通过其字符串名称加载类,但类在类路径上不可用时抛出。
- 一个示例可以是您代码中的
Class.forName("com.mysql.jdbc.driver");
方法调用,然而,您的类路径上没有com.mysql.jdbc.driver
可用。
Couldn't find or load main class XYZ
- 这是一个错误,表示您指示JVM运行的类没有必须的入口public static void main(String[] args)
方法,其中一个原因可能导致该错误:
- 您没有提供主类的正确完全限定名称;
- 主方法没有使用正确的签名定义;
- 您搞乱了包装/未使用
super.sub.grandchild.MainClass
名称运行程序; - 您在类名后面有
.class
后缀,应该删除它。
英文:
NoClassDefFoundError
- is a run-time error thrown when the bytecode of the class (i.e. corresponding .class
file) cannot be found at run-time.
- Imagine you've compiled classes
A.java
andB.java
andA
is usingB
; however, later on, you've removed compiledB.class
file. So, compilation went fine, but afterwards, actual bytecode of the class definition has been removed, as you removedB.class
. Now, if you run yourA
(which usesB
), you will end up havingjava.lang.NoClassDefFoundError
.
ClassNotFoundException
- is a checked exception thrown when your application tries to load the class through its String name, but the class isn't available on classpath.
- An example can be
Class.forName("com.mysql.jdbc.driver");
method call in your code, when, however, you don't havecom.mysql.jdbc.driver
available on the classpath.
Couldn't find or load main class XYZ
- is an error, indicating, that the class you instruct JVM to run, doesn't contain the must have entry-point public static void main(String[] args)
method and one of these reasons can lead to that error:
- you don't provide a correct Fully Qualified Name of your main class;
- main method is not defined with the correct signature;
- you messed up the packaging / you don't run the program with
super.sub.grandchild.MainClass
name; - you have
.class
postfix after your classname, which you should remove.
答案3
得分: 1
ClassNotFoundException
和 NoClassDefFoundError
都是由于 JVM 无法加载特定文件而引起的,但它们的原因不同。
Java 运行时在尝试在运行时加载类并且类名是在运行时提供的时候抛出 ClassNotFoundException
。而在 NoClassDefFoundError
的情况下,类在编译时存在,但在运行时 Java 无法在 Java 类路径中找到它。
"找不到或无法加载主类"
的错误消息可能由各种原因引起,它也可能由 ClassNotFoundException
或 NoClassDefFoundError
引起。
错误: 找不到或无法加载主类 ClassName.class
原因: java.lang.ClassNotFoundException: ClassName.class
英文:
Both ClassNotFoundException
and NoClassDefFoundError
are caused when the JVM is not able to load a particular file, but their cause differs.
Java runtime throws ClassNotFoundException
while trying to load a class at runtime only and the name was provided during runtime. In the case of NoClassDefFoundError
, the class was present at compile-time, but Java runtime could not find it in Java classpath during runtime.
Couldn't find or load main class
error message can be caused by various reasons, it can also be caused by ClassNotFoundException
or NoClassDefFoundError
.
Error: Could not find or load main class ClassName.class
Caused by: java.lang.ClassNotFoundException: ClassName.class
答案4
得分: 1
NoClassDefFoundError
当 JVM 或 ClassLoader
尝试加载一个类(作为正常方法调用的一部分,或者作为使用 new
表达式创建新实例的一部分)时,如果在当前执行类编译时存在,但在加载时找不到该类,则会发生此错误。
ClassNotFoundException
如文档中所述,在以下情况下会抛出此异常:当应用程序尝试通过其字符串名称加载类时,使用:
Class
类中的forName
方法。ClassLoader
类中的findSystemClass
方法。ClassLoader
类中的loadClass
方法。
这意味着 JVM 不知道在当前执行类编译时是否存在要加载的类。
英文:
NoClassDefFoundError
This occurs when the JVM or a ClassLoader
tries to load a class (as part of a normal method call or as part of creating a new instance using the new
expression) but it can not be found while it existed when the currently executing class was compiled.
ClassNotFoundException
As mentioned in the documentation, it is thrown when an application tries to load a class through its string name using:
- The
forName
method in classClass
. - The
findSystemClass
method in classClassLoader
. - The
loadClass
method in classClassLoader
.
It means that it is unknown to JVM whether the class (which is to be loaded) existed when the currently executing class was compiled.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论