java.lang.NoClassDefFoundError
NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available during compile time. This exception is different from ClassNotFoundException where either the jar file is missing or the class file is missing.
Here it is unable to initialize the class while loading it or the dependent class is missing or the dependent jar/class is not able to initialize. So need to check the dependants as well.
There can be any type of problem for this exception , Some possible causes of the exception are
1. When the static blocks or static variables of the class fails at the runtime. Also known as ExceptionInInitializerError .
Suppose there is an static bloc which tries to load some properties file reading from some location and that file does not exist.
When this class is invoked at the runtime, it will throw NoClassDefFoundError as the Class's static block was failed.
public class Employee
{
static
{
InputStream input = null;
input = new FileInputStream(new File("D:/test.properties")));
props.load(w_Input);
input.close();
}
}
And "D:/test.properties" this file does not exist then it will throw ExceptionInInitializerError which in turn will cause NoClassDefFoundError when trying to create an object of Employee class
public class Demo
{
public static void main(String args[])
{
try
{
Employee emp = new Employee();//this will throw ExceptionInInitializerError
}
catch(exception e)
{
}
Employee emp = new Employee();//this will throw NoClassDefFoundError
}
}
Or Another example of static variable failure
public class Employee
{
static int empid = 6/0; // static fail (ExceptionInInitializerError )
}
2. Or there must be some init api in the class which failed.
Here also there are chances that code was expecting something at the runtime which was not fulfilled
3. Or the dependent jar/class file was missing or the dependent class file was not able to get initialized.
This Exception is different from ClassNotFoundException where the class is actually missing from the class path there we need to add it in the classpath or add its jar file.
4. There was an dependent class file, that was present at the compile time but was missing at the run time.
5. Permission issue of the jar file.
Here are some other possible causes of this error:
1) Class is not available in Java Classpath.
2) You might be running your program using jar command and class was not defined in manifest file's ClassPath attribute.
3) Any startup script is overriding Classpath environment variable.
How to resolve it??
1- Class is not in Classpath, there is no sure shot way of knowing it but many a times you can just have a look to print System.getproperty("java.classpath")and it will print the classpath from there you can at least get an idea of your actual runtime classpath.
2- Just try to run with explicitly -classpath option with the classpath you think will work and if its working then it's sure short sign that some one is overriding java classpath.
3- Please check have similar versions of JRE at compiletime and runtime.
4- If you are getting NoClassDefFoundError for some external jar file that you have added to the project, try adding the jar file in lib folder and add it to the classpath by Properties >> Java Build Path >> Add Variable >> Configure Variables >> New Variable Entry. And rebuild.
If its a struts app
1- Check to make sure struts.jar is in your WEB-INF/lib directory
2- Check to make sure that the commons-xxx.jar files and the struts.jar file are for the same version of struts, meaning that they all came from the same struts download package.
3- Make sure that there is not another struts.jar or one of the commons-xxx.jar files being loaded by another Class Loader. This could happen if there's a copy of one of these jar files in some common area used by your App Server.
Comments
Post a Comment