4

I explored multiple sites but could not actually understand difference between them.I would like to know the exact difference between three.

4 Answers4

3

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

JCWasmx86
  • 3,308
  • 2
  • 9
  • 26
2

NoClassDefFoundError - is a run-time error, thrown when the Definition of the class (which is .class file) cannot be found at run-time.

  • Imagine you've compiled class A.java alongside other files of your project; however, later on, you've removed compiled A.class file. So, compilation went fine, but actual bytecode of the class definition is absent, as A.class has been removed.

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 class-path.

  • 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 in your project.

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 reasons for this can be different, mainly one from this list:

  • 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.
Giorgi Tsiklauri
  • 8,246
  • 8
  • 35
  • 56
1

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
Deepak Patankar
  • 2,698
  • 2
  • 15
  • 28
1

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.

Arvind Kumar Avinash
  • 62,771
  • 5
  • 54
  • 92