Skip to main content

ClassNotFoundException and NoClassDefFoundError in Java

As the name suggest ClassNotFoundException is of Exception type and the NoClassDefFoundError is a type of Error. ClassNotFoundException can be handled inside code while NoClassDefFoundError can't. ClassNotFoundException and NoClassDefFoundError occur when the particular required class is not found during run-time. It occurs in different scenarios.

ClassNotFoundException 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.

but not available during run time. For example, you might have come across this exception when you would have tried to connect to MySQL or Oracle databases and you have not updated the classpath with required JAR files. Most of the time, this exception occurs when you try to run an application without updating the classpath with required JAR files.

For example, the below program will throw ClassNotFoundException if the mentioned class “oracle.jdbc.driver.OracleDriver” is not found in the classpath.

public class Test
{
    public static void main(String[] args)
    {
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        }catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}

If you run the above program without putting the required JAR files inside classpath, you will get the exception.


NoClassDefFoundError is an error that is thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found. It may also occur if the required class is compiled in higher version of java compiler and the class referring is compiled in lower version of jdk and running in lower version of JRE environment.

For example, compile the program below.

class UseThis{
}
public User
{
    public static void main(String[] args)
    {
        UseThis test = new UseThis ();
    }
}


When you compile the above program, two .class file will be generated. One is UseThis.class and another one is User.class. If you remove the UseThis.class file and try to run the User.class, JRE will throw NoClassDefFoundError.

Comments

Popular posts from this blog

StackOverFlowError and OutOfMemoryError in java

There are two area inside java virtual machine's memory the heap and the  stack . The  stack  memory is used to store local variables and function call while heap memory is used to store objects in  Java The most common cause of StackOverFlowError is too deep or infinite recursion or many local objects / variables creation inside function call in  Java.  According to the java source documentation,  Java throws  java.lang.StackOverflowError   when a stack overflow occurs because an application recurses too deeply. JVM has a given memory allocation for each stack of each thread, and if an attempt to call a method happens to fill this memory, JVM throws an error. Just like it would do if we try to write at index N of an array of length N.  The point to be noted here is that - These are errors not an exceptions. No memory corruption happens due to the error. Stack can not write into the heap space. A StackOverflowError i...

Job Sequencing with Deadlines

Given a set of n jobs Each job i has an integer deadlines di>=0 and a profit pi>0 All jobs requires only one unit time to complete Only one machine is available for processing jobs For job i the profit pi is earned if the job is completed by its deadline.