What is Difference between ClassNotFoundException vs NoClassDefFoundError vs Could not find or load main class XYZ?

huangapple go评论61阅读模式
英文:

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

  1. Class类中的forName方法。
  2. ClassLoader类中的findSystemClass方法。
  3. 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 意味着许多情况:

  1. 您指定了错误的类(拼写错误)。
  2. 类文件中指定的类(不)在包中。(例如,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:

  1. The forName method in class Class.
  2. The findSystemClass method in class ClassLoader .
  3. 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:

  1. You specified the wrong class (Typo)
  2. 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 be java a.b.c)

More information/causes: https://stackoverflow.com/a/18093929/13912132

答案2

得分: 2

NoClassDefFoundError - 在运行时抛出的运行时错误,当类的字节码(即相应的.class文件)在运行时无法找到时会抛出。

  • 想象一下,您已经编译了类A.javaB.java,并且A正在使用B;然而,稍后您删除了已编译的B.class文件。因此,编译顺利进行,但后来,类定义的实际字节码已被删除,因为您删除了B.class。现在,如果运行使用BA,您将最终遇到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 and B.java and A is using B; however, later on, you've removed compiled B.class file. So, compilation went fine, but afterwards, actual bytecode of the class definition has been removed, as you removed B.class. Now, if you run your A (which uses B), you will end up having java.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 have com.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

ClassNotFoundExceptionNoClassDefFoundError 都是由于 JVM 无法加载特定文件而引起的,但它们的原因不同。

Java 运行时在尝试在运行时加载类并且类名是在运行时提供的时候抛出 ClassNotFoundException。而在 NoClassDefFoundError 的情况下,类在编译时存在,但在运行时 Java 无法在 Java 类路径中找到它。

"找不到或无法加载主类" 的错误消息可能由各种原因引起,它也可能由 ClassNotFoundExceptionNoClassDefFoundError 引起。

错误: 找不到或无法加载主类 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

如文档中所述,在以下情况下会抛出此异常:当应用程序尝试通过其字符串名称加载类时,使用:

  1. Class 类中的 forName 方法。
  2. ClassLoader 类中的 findSystemClass 方法。
  3. 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:

  1. The forName method in class Class.
  2. The findSystemClass method in class ClassLoader.
  3. The loadClass method in class ClassLoader.

It means that it is unknown to JVM whether the class (which is to be loaded) existed when the currently executing class was compiled.

huangapple
  • 本文由 发表于 2020年8月2日 00:30:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63207516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定