Monday, 25 April 2016

What are the errors related to Classpath in Java ?


ClassNotFoundException
It is an Exception and will be thrown when Java program dynamically tries to load a particular Class at Runtime and don’t find that on Java classpath.
Example : When you try to load JDBC driver by using Class.forname(“driver name”).

This error essentially comes when Java try to load a class using forName() or by loadClass() method of ClassLoader.
Presence of that class on Java classpath is not checked on compile time. So even if those classes are not present on Java classpath your program will compile successfully and only fail when you try to run.


NoClassDefFoundError
This error is more critical than ClassNotFoundException (which is an exception and recoverable).
NoClassDefFoundError comes when a particular class was present in Java Classpath during compile time but not available during run-time.
Example : Using log4j.jar for logging purpose and forgot to include log4j.jar on classpath in java during run-time.

Keyword here is “class present at compile time but not available on run-time”.

This is normally occurring due to any method invocation on a particular class which is part of library and not available on classpath in Java.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.