What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?

前端 未结 15 2713
忘了有多久
忘了有多久 2020-11-21 05:27

What is the difference between NoClassDefFoundError and ClassNotFoundException?

What causes them to be thrown? How can they be resolved?

15条回答
  •  花落未央
    2020-11-21 05:55

    Given the Class loader sussystem actions:

    http://www.artima.com/insidejvm/ed2/images/fig7-1.gif

    This is an article that helped me a lot to understand the difference: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html

    If an error occurs during class loading, then an instance of a subclass of LinkageError must be thrown at a point in the program that (directly or indirectly) uses the class or interface being loaded.

    If the Java Virtual Machine ever attempts to load a class C during verification (§5.4.1) or resolution (§5.4.3) (but not initialization (§5.5)), and the class loader that is used to initiate loading of C throws an instance of ClassNotFoundException, then the Java Virtual Machine must throw an instance of NoClassDefFoundError whose cause is the instance of ClassNotFoundException.

    So a ClassNotFoundException is a root cause of NoClassDefFoundError.
    And a NoClassDefFoundError is a special case of type loading error, that occurs at Linking step.

提交回复
热议问题