Add to Google Reader or Homepage

Exception in thread "main" java.lang.ClassNotFoundException

ClassNotFoundException

 

The ClassNotFoundException is 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 no definition for the class with the specified name could be found,ie if a class with the given name is not available during loading then this exception would be thrown.

Example:

 

import java.util.*;
import java.io.*;
 import java.lang.reflect.*;
 public class Ex
 {
 public static void main(String args[])
 {
 String name;
 System.out.println("Enter the class to be loaded");
 Scanner in=new Scanner(new InputStreamReader(System.in));
 name=in.next();
try
{
Class cl=Class.forName(name);
System.out.println();
Constructor[] constructors = cl.getDeclaredConstructors();
for (Constructor c : constructors)
System.out.println(c.getName());
Method[] methods = cl.getDeclaredMethods();
for (Method m : methods)
System.out.println( m.getName());
}

catch(ClassNotFoundException e)
{
e.printStackTrace();
}

}
}



The above program will ask the user to enter a class name to display its constructors and methods.If a class with the given name is not available then the ClassNotFoundException will be thrown.
 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top