5

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

4 Answers4

4

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,473
  • 2
  • 11
  • 29
2

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.
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
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
  • 3,076
  • 3
  • 16
  • 35
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
  • 71,965
  • 6
  • 74
  • 110