Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Very Strange java.lang.NoClassDefFoundError. Class exists in classpath

843810Dec 19 2007 — edited Dec 25 2007
Dear 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");
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 22 2008
Added on Dec 19 2007
1 comment
332 views