ClassNotFoundException
is a run time exception which is thrown when an application tries to load a
class at run time using Class.forName() or loadClass() or findSystemClass()
methods and the class with specified name are not found in the classpath.
For
example, you may have come across this exception when you try to connect to
MySQL or Oracle databases and you have not updated the classpath with required
JAR files. In most of time, this exception occurs when you try to run an
application without updating the classpath with required JAR files.
For
example, below program will throw ClassNotFoundException if the mentioned class
“oracle.jdbc.driver.OracleDriver” is not found in the classpath.
public
class MainClass
{
public static void main(String[] args)
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
If
you run the above program without updating the classpath with required JAR
files, you will get the exception like below,
NoClassDefFoundError
is an error which is thrown when Java Runtime System tries to load the
definition of a class and class definition is no longer available. The required
class definition was present at compile time but it was missing at run
time.
class
A
{
}
public
class B
{
public static void main(String[] args)
{
A a = new A();
}
}
When
you compile the above program, two .class files will be generated. One is A.class and
another one isB.class. If you remove the A.class file
and run the B.class file, Java Runtime System will throw
NoClassDefFoundError like below,
No comments:
Post a Comment