ClassNotFoundException vs. NoClassDefFoundError java

Please follow and like us:
5

This is very common interview question in core java. Here is the difference between the two. Both of them occur at run-time in application but they occur in different scenario.

  1. ClassNotFoundException:- ClassNotFoundException is a runtime exception which occurs when we try to load a class at runtime using Class.forName() method but dependent jar is not present in the classPath. Popular example for this could be when we connect to Database using JDBC we use Class.forName() to load the Driver Class to get the connection.

    if mysql jar is not present in the classPath then we get ClassNotFoundException.
  2.  NoClassDefFoundError:- NoClassDefFoundError is an error which occurs at runtime when class is available at compile time and but at runtime class is not available.

    Now if we compile the above given program then we get two class files A.class and B.class but if delete A.class then we will get NoClassDefFoundError.

Anuj Aneja did his Graduation from a reputed college in India YMCA Institute of Engineering. He is having more than 7 years of experience in various domains like: HealthCare, Telecom, Financial Sector. Till now, He has worked on various technologies like: Java, Python, Hadoop Ecosystem and Spark. He has vast experience in designing both Web application as well as High performance Scalable applications. He has also worked on both SQL and NoSQL database management systems like: Mysql, Oracle, Mongodb.

Please follow and like us:
5

Leave a Reply

Your email address will not be published. Required fields are marked *