Searching...
Sunday, 27 April 2014

ClassNotFoundException v/s NoClassDefFoundError

04:17

ClassNotFoundException

ClassNotFoundException Thrown when an application tries to load in a class through its name as String. (When Dynamic loading).
Here is one simple example to throw class not found Exception

public class ClassNotFoundTest {
	public static void main(String[] args) {
		try {
				ClassLoader myClassLoader = ClassLoader.getSystemClassLoader();
				String className = "testclass";
				Class<?> myClass = myClassLoader.loadClass(className);
				System.out.println("Successfully loaded"+myClass.getSimpleName());	
			}
			catch (ClassNotFoundException  e) {
				System.out.println("Exception message   "+e);
			}
	}
}


Exception message   java.lang.ClassNotFoundException: testclass



NoClassDefFoundError


NoClassDefFoundError Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class 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.

0 comments: