I explored multiple sites but could not actually understand difference between them.I would like to know the exact difference between three.
4 Answers
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
 
    
    - 3,473
- 2
- 11
- 29
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.javaandB.javaandAis usingB; however, later on, you've removed compiledB.classfile. 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.driveravailable 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.MainClassname;
- you have .classpostfix after your classname, which you should remove.
 
    
    - 9,715
- 8
- 45
- 66
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
 
    
    - 3,076
- 3
- 16
- 35
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 forNamemethod in classClass.
- The findSystemClassmethod in classClassLoader.
- The loadClassmethod 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.
 
    
    - 71,965
- 6
- 74
- 110