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

前端 未结 15 2707
忘了有多久
忘了有多久 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 06:16

    Add one possible reason in practise:

    • ClassNotFoundException: as cletus said, you use interface while inherited class of interface is not in the classpath. E.g, Service Provider Pattern (or Service Locator) try to locate some non-existing class
    • NoClassDefFoundError: given class is found while the dependency of given class is not found

    In practise, Error may be thrown silently, e.g, you submit a timer task and in the timer task it throws Error, while in most cases, your program only catches Exception. Then the Timer main loop is ended without any information. A similar Error to NoClassDefFoundError is ExceptionInInitializerError, when your static initializer or the initializer for a static variable throws an exception.

提交回复
热议问题