Add to Google Reader or Homepage

Exception in thread "main" java.lang.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.



import java.util.*;
 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(;;
Class cl=Class.forName(name);
Constructor[] constructors = cl.getDeclaredConstructors();
for (Constructor c : constructors)
Method[] methods = cl.getDeclaredMethods();
for (Method m : methods)
System.out.println( m.getName());

catch(ClassNotFoundException e)


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