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

Optimal Binary Search using Dynamic Programming

An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum. If the probabilities of searching for elements of a set are known from accumulated data from past searches, then Binary Search Tree (BST) should be such that the average number of comparisons in a search should be minimum. eg. Lets the elements to be searched are A, B, C, D and probabilities of searching these items are 0.1, 0.2, 0.4 and 0.3 respectively. Lets consider 2 out of 14 possible BST containing these keys. Figure 1 Figure 2 Average number of comparison is calculated as sum of level*probability(key element) for each element of the tree. Lets the level of tree start from 1. Therefore, for figure 1 -     Average number of comparison = 1*0.1 +2*0.2 +3*0.4 +4*0.3  = 2.9                                  ...

Important points on Classes and Methods in Java as per Java Language Specification

Class Declarations: A class declaration specifies a new named reference type. There are two kinds of class declarations: normal class declarations and enum declarations. It is a compile-time error if a class has the same simple name as any of its enclosing classes or interfaces. Class Modifiers: A class declaration may include class modifiers. The access modifier public pertains only to top level classes and member classes, not to local classes or anonymous classes. The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration. The modifier static pertains only to member classes, not to top level or local or anonymous classes. It is a compile-time error if the same keyword appears more than once as a modifier for a class declaration. abstract Classes: An abstract class is a class that is incomplete, or to be considered incomplete. It is a compile-time error if an attempt is made to create an instance o...

Web Service Explorer (An Amazing tool in eclipse IDE)

Web Service Explorer (An Amazing tool in eclipse IDE) Developer or user who wants to use the web-service has to consume the web-service and invoke the method of the service he/she wants to. Thus there are many potential ways to test the we-service exposed. • write a CICS program that invokes the Web Service • consume the WSDL and make a client project in eclipse IDE or My-Eclipse and invoke the service • generate a Java program using Rational Application Developer (RAD) or WebSphere Developer for  zSeries (WDz), • use the debugger that comes with WDz, • use the supplied Web Services Explorer that comes with RAD and WDz, • use the TCP/IP Monitor that comes with RAD and WDz, or a combination of these. Apart from these, one method to test the web-service on the ease using the web service explorer tool available in eclipse IDE. For the tutorial of how to use visit the below official link of tutorial. http://www.eclipse.org/webtools/jst/components/ws/M4/tu...