Very Strange java.lang.NoClassDefFoundError. Class exists in classpath
843810Dec 19 2007 — edited Dec 25 2007Dear Forum:
Occasionally, I run into strange java.lang.NoClassDefFoundErrors.
Here is an example: I have two different classes Class1.class and Class2.class in different jars. Both use a common class: AppContext in third jar file.
If Class1 tries to use AppContext, it works just fine. However, if Class1 instantiates Class2 and Class2 tries to use AppContext, you get the nasty java.lang.NoClassDefFoundError, in the same thread.
After a struggle and trying out different orders in class / library paths, the error goes away. However, I do want to know what is the better way to understand and resolve this issue?
Here is a sample code, for illustration purposes only:
File:Class1.java_
--------------------
Public class Class1
{
Public Class1 ()
{
AppContext.getLogger().logDebug(�test1�);
//the above line works just fine.
try
{
Class2 something = new Class2();
}
catch(Error ex)
{
//here we get a java.lang.NoClassDefFoundError for AppContext, which works just fine from Class1.java
ex.printstackTrace();
}
}
}
File:Class2.java:_
----------------------------------------
Public class Class2
{
Public Class2()
{
AppContext.getLogger().logDebug("test2");
}
}