Feb 8, 2016

ClassNotFoundException Vs NoClassDefFoundError In Java

Below are some differences between ClassNotFoundException and NoClassDefFoundError:

ClassNotFoundException
NoClassDefFoundError 
It is a checked exception. It is of type java.lang.Exception.
It is an error. It is of type java.lang.Error.
It requires mandatory handling using either try with catch block or try with the finally block, failure to do so will result in compile time error.
It doesn't require try-catch or finally block.
It occurs when an application tries to load a class at run time which is not updated in the classpath.
It occurs when java runtime system doesn’t find a class definition, which is present at compile time, but missing at run time.
It is thrown by the application itself. It is thrown by the methods like Class.forName(), ClassLoader .loadClass() and ClassLoader .findSystemClass().
It is thrown by the Java Runtime System.
It occurs when classpath is not updated with required JAR files.
It occurs when required class definition is missing at run time.

ClassNotFoundException can be created using following program-

package com.javaterritory;
 
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
 
public class ClassNotFoundExceptionTest {
 
       public static void main(String args[]) {
              File file = new File("c:\\test\\");
 
              try {
                     // Convert File to a URL
                     URL url = file.toURL();
                     URL[] urls = new URL[] { url };
 
                     // Create a new class loader
                     ClassLoader cl = new URLClassLoader(urls);
 
                     // Load in the class : ClassNotFoundExample.class
                     Class cls = cl.loadClass("com.javaterritory.ClassNotFoundExample");
              } catch (MalformedURLException e) {
                     // e.printStackTrace();
              } catch (ClassNotFoundException e) {
                     e.printStackTrace();
              }
       }
}

 To avoid this exception, user should check the following - 
-     Make sure that Class is available in the logical class path of the class loader associated with
the class.        
-     Make sure that Class Loader API is used properly i.e. whether a wrong class Loader is engaged in Class.forname(). 
-     Make sure that dependent class of the class being loaded is visible to the class loader.

NoClassDefFoundError can be created using following program – 


package com.javaterritory;

public class NoClassDefFoundErrorTest {
 public static void main(String[] args) {
  Shape obj = new Square();
  obj.print();
 }
}

class Square extends Shape {
 public void print() {
  System.out.println("I am Square");
 }
}

abstract class Shape {
 abstract void print();
}

After compiling the above program, delete the Shape.class file. Try to execute the code afterwards, error be shown on console:

Exception in thread "main" java.lang.NoClassDefFoundError: Shape

Possible reasons of the error- 
-      Bad format of the class 
-      The version number of a class not matching
-      This can happen in the distribution or production of JAR files, where not all the required class files were included.

If you liked the above post, join at +Java Territory to stay updated with latest articles.

0 comments:

Post a Comment