. Advertisement .
..3..
. Advertisement .
..4..
I’m building a new program, but when I run it, an error pops up. The error displayed is as follows:
java.lang.NoClassDefFoundError: Could not initialize class XXX
I have tried several workarounds, but they still do not get the desired results. If you have come across this situation and have a solution for the “java lang noclassdeffounderror could not initialize class” problem, pls let me know. Here is what I do:
public class PropHolder {
public static Properties prop;
static {
//code for loading properties from file
}
}
// Referencing the class somewhere else:
Properties prop = PropHolder.prop;
Thanks!
The cause: I think this error occurs in here:
It appears that an uncaught exception occurred, which was propagated to the ClassLoader that was attempting to load the class. Either that or it happened when the
PropHolder.prop
static variable was created.The solution: You’ll need to figure out what exception the
static
block is throwing. To debug it, wrap the entire block in atry/catch(Exception e)
and log the exception. That exception will have to be fixed. Typically, the exception will be logged, but it may be difficult to locate because it is logged during classloading, which can happen very early in the process.A
java.lang.NoClassDefFoundError
does not mean your class is missing. In that case, you would get ajava.lang.ClassNotFoundException
. When trying to read the class, the ClassLoader encountered an error when reading the class definition.Try/catch in your static initializer to see the exception. If you find files in your local environment that differ from what you are used to, it is likely the source of the problem. ).