You are on page 1of 4

3 ways to solve java.lang.

NoClassDefFoundError in Java J2EE


What is Exception in thread "main" java.lang.NoClassDefFoundError? I know how frustrating is to see Exception in thread "main" java.lang.NoClassDefFoundError Which is a manifestation of NoClassDefFoundError in Java I have seen it couple of times and spent !uite a lot time initiall" to figure out what is wrong which class is missing etc# First mistake I did was mingling $ava#lang#ClassNotfoundException and NoClassDefFoundError in realit" the" are totall" different and second mistake was using trial and error method to solve this java.lang.NoClassDefFoundError instead of understanding wh" NoClassDefFoundError is coming what is real reason %ehind NoClassDefFoundError and how to resolve this# In this Java tutorial I have tried to rectif" that mistakes and uncover some secrets of NoClassDefFoundError in Java and will share m" experience around it# NoClassDefFoundError is not something which cannot %e resolved or hard to resolve it&s $ust its manifestation which pu''les most of Java developer# (his is the most common error in Java development along with java.lang.OutOfMemoroyError: Java heap space and $ava#lang#)ut)f*emor"Error+ ,erm-en space .n"wa" let&s see Wh" NoClassDefFoundError comes in Java and what to do to resolve NoClassDefFoundError in Java#

What is reason of NoClassDefFoundError in Java? NoClassDefFoundError in Java comes when Java /irtual *achine is not a%le to find a particular class at runtime which was availa%le during compile time# For example if we have a method call from a class or accessing an" static mem%er of a Class and that class is not availa%le during run0time then J/* will throw NoClassDefFoundError# It&s important to understand that this is different than ClassNotFoundExcept on which comes while tr"ing to load a class at run0time onl" and name was provided during runtime not on compile time# *an" Java developer mingle this two Error and gets confused# In short NoClassDefFoundError will come if a class was present during compile time %ut not availa%le in $ava classpath during runtime# Normall" "ou will see %elow line in log when "ou get NoClassDefFoundError+

Exception in thread "main" java.lang.NoClassDefFoundError


Exception in thread 1main2 simpl" indicate that its 1main2 thread which is not a%le to find a particular class it could %e an" thread so $ust don&t worr" # Difference %etween this error coming in main thread and other thread is when Exception in thread 1main2 comes program crashes or shut it self down as opposed to other thread in which case "our program will continue to run#

Difference between java.lang.NoClassDefFoundError and ClassNotFoundException in Java


*an" a times we confused ourselves with java.lang.ClassNotFoundExcept on and java.lang#NoClassDefFoundError though %oth of them related to Java Classpath the" are completel" different to each other# ClassNotFoundException comes when J/* tries to load a class at runtime d"namicall" means "ou give the name of class at runtime and then J/* tries to load it and if that class is not found in classpath it throws $ava#lang#ClassNotFoundException# While in case of NoClassDefFoundError the pro%lematic class was present during Compile time and that3s wh" program was successfull" compile %ut not availa%le during runtime %" an" reason# NoClassDefFoundError is easier to solve than ClassNotFoundExcept on in m" opinion %ecause here we know that Class was present during %uild time %ut it totall" depends upon environment if "ou are working in J4EE environment than "ou can get NoClassDefFoundError even if class is present %ecause it ma" not %e visi%le to corresponding Class5oader# 6ee m" post NoClassDefFoundError vs ClassNotFoundExcept on in Java for more details#

o! to resolve java.lang.NoClassDefFoundError"

)%vious reason of NoClassDefFoundError is that a particular class is not availa%le in Classpath so we need to add that into Classpath or we need to check wh" it&s not availa%le in Classpath if we are expecting it to %e# (here could %e multiple reasons like+ 78 Class is not availa%le in Java Classpath# 48 9ou might %e running "our program using $ar command and class was not defined in manifest file3s Class,ath attri%ute# :8 .n" start0up script is overriding Classpath environment varia%le# ;8 <ecause NoClassDefFoundError is a su% class of java.lang.! n"ageError it can also come if one of it dependenc" like native li%rar" ma" not availa%le# ;8 Check for java.lang.Except on#n#n t al $erError in "our log file# NoClassDefFoundError due to failure of static initiali'ation is !uite common# =8 If "ou are working in J4EE environment than visi%ilit" of Class among multiple Classloaders can also cause java.lang.NoClassDefFoundError see examples and scenario section for detailed discussion# We will now see couple of example and scenarios when java.lang.NoClassDefFoundError has came %efore and how its %een resolved# (his can help "ou to trou%leshoot root cause of NoClassDefFoundError in Java application#

NoClassDefFoundError in Java - Example and Scenarios


7# 6imple example of NoClassDefFoundError is class %elongs to a missing J.> file or J.> was not added into classpath or sometime $ar3s name has %een changed %" someone like in m" case one of m" colleague has changed ti#co.jar into ti#co$v%.jar and %" program is failing with java.lang.NoClassDefFoundError and I was wondering what3s wrong# 4# Class is not in Classpath there is no sure shot wa" of knowing it %ut man" a times "ou can $ust have a look to print %ystem.getproperty&'java.classpath'(and it will print the classpath from there "ou can at least get an idea of "our actual runtime classpath# :# Just tr" to run with explicitl" )classpath option with the classpath "ou think will work and if its working then it3s sure short sign that some one is overriding java classpat # NoClassDefFoundError in Java due to Exception in &tatic 'nitiali(er #loc) ;8 (his is another common reason of java.lang.NoClassDefFoundError when "our class perform some static initiali'ation in static %lock like man" 6ingleton classes initiali'ed itself on static %lock to take advantage of thread0safet" provided %" J/* during class initiali'ation process and if static %lock throw an Exception the class which is referring to this class will get NoclassDefFoundError in Java# If "ou look at "our log file "ou should watch for an" java.lang.Except on#n#n t al $erError %ecause that could trigger java.lang.NoClassDefFoundError: Could not n t al $e class on other places# 5ike in %elow code example During class loading and initiali'ation *ser class is throwing Except on from static initiali'er %lock which trigger Except on#n#n t al $erError during first time loading of *ser class in response to new *ser&( call# 5ater rest of new *ser&( are failing as $ava.lang.NoClassDefFoundError# situation gets worst if original Except on#n#n t al $erError which is root cause here is silentl" eaten %" an" code# Code Example of NoClassDefFoundError due to 6tatic %lock Exception+ +,, , Java program to demonstrate ho- fa lure of static initialization su.se/uently cause , java.lang.NoClassDefFoundError n Java. , 0author Jav n 1aul ,+ public class NoClassDefFoundErrorDue2o%tat c#n tFa lure 3

public static void ma n&String args45(3 List6*ser7 users 8 new ArrayList6*ser7&9(: for&int 8;: 69: <<(3 try3 users.add&new *ser&String.valueOf& (((: ++- ll throNoClassDefFoundError =catc &! rowable t(3 t.pr nt%tac"2race&(: = = = = class *ser3 private static String *%E>?#D 8 get*ser#d&(: public *ser&String d(3 t is.*%E>?#D 8 d: = private static String get*ser#d&( 3 t row new "unti#eE$ception&'*ser#d Not found'(: = = %utput
java.lang.E$ception&n&nitializerError at test ng.NoClassDefFoundErrorDue2o%tat c#n tFa lure.ma n&NoClassDefFoundErrorDue2o%tat c#n tFa lure.java:9@( Caused .y: java.lang."unti#eE$ception: *ser#d Not found at test ng.*ser.get*ser#d&NoClassDefFoundErrorDue2o%tat c#n tFa lure.java:AB( at test ng.*ser.6cl n t7&NoClassDefFoundErrorDue2o%tat c#n tFa lure.java:@C( ... B more java.lang.NoClassDefFoundError: Could not n t al $e class test ng.*ser at test ng.NoClassDefFoundErrorDue2o%tat c#n tFa lure.ma n&NoClassDefFoundErrorDue2o%tat c#n tFa lure.java:9@(

=8 6ince NoClassDefFoundError is a also a ! n"ageError which arises due to dependenc" on some other class "ou can also get java.lang.NoClassDefFoundError if "our program is dependent on native li%rar" and corresponding dll is not there# >emem%er this can also trigger java.lang.UnsatisfiedLinkError: no dll in java.li rary.!at" E#$e!tion Java. In order to solve this keep "our dll along with J.># ?8 If "ou are using .N( %uild file create J.> and manifest file than its worth noting to de%ug till that level to ensure that .N( %uild script is getting correct value of classpath and appending it to man fest.mf file. @8 ,ermission issue on J.> file can also cause NoClassDefFoundError in Java# If "ou are running "our Java program in multi0user operating s"stem like 5inux than "ou should %e using application user id for all "our application resources like J.> files li%raries and configuration# If "ou are using shared li%rar" which is shared among multiple application which runs under different users then "ou ma" run with permission issue like J.> file is owned %" some other user and not accessi%le to "our application# )ne of our reader 1it&s me said2 faced java.lang.NoClassDefFoundError due to this reason# 6ee his comment also# A8 ("po on B*5 Configuration can also cause NoClassDefFoundError in Java# .s most of Java frameworks like 6pring 6truts the" all use xml configuration for specif"ing %eans# <" an" chance if "ou put the %ean name wrong it ma" surface as java.lang.NoClassDefFoundError while loading other class which has dependenc" on wrongl" named %ean# (his is !uite common on 6pring */C framework and .pache 6truts where "ou get tons of E$ception in t read '#ain' java.lang.NoClassDefFoundError * while deplo"ing "our W.> or E.> file# C8 .nother example of java.lang.NoClassDefFoundError as mentioned %" our reader Nick is that when "our compiled class which is defined in a package doesn&t present in same package while loading like in case of JDpplet it will throw NoClassDefFoundError in Java# .lso see Nick&s comment on this error#

7D8 java.lang.NoClassDefFoundError can %e caused due to multiple classloaders in J4EE environments# 6ince J4EE doesn&t mention standard class0loader structure and it depends upon different vendors like 2omcat Ee.!og c We%6phere on how the" load different components of J4EE like W.> file or EJ<0J.> file# In order to trou%leshoot NoClassDefFoundError in J+EE application knowledge of Eow Class5oader works in Java is mandator"# Just to recap Clas5oader works on three principle delegation visi%ilit" and uni!ueness# Delegation means ever" re!uest to load a class is delegated to parent classloader visi%ilit" means a%ilit" to found classes loaded %" classloader all child classloader can see classes loaded %" parent classloader %ut parent classloader can not see the class loaded %" child classloaders# Fni!ueness enforce that class loaded %" parent will never %e reloaded %" child clasloaders# Now suppose if a class sa" Fser is present in %oth W.> file and EJ<0J.> file and loaded %" W.> classloader which is child classloader which loads class from EJ<0J.># When a code in EJ<0 J.> refer to this Fser class Classloader which loaded all EJ< class doesn&t found that %ecause it was loaded %" W.> classloader which is child of it# (his will result in java.lang.NoClassDefFoundError for Fser class# .lso If class is present in %oth J.> file and "ou will call e!uals method to compare those two o%$ect it will result in ClassCastExcept on as o%$ect loaded %" two different classloader can not %e e!ual# 778 6ome of reader of this %log also suggested that the" get Exception in thread "main" $ava#lang#NoClassDefFoundError+ comGsunGtoolsG$avacG*ain this error means either "our Classpath ,.(E or J./.HE)*E is not setup properl" or JDI installation is not correct# which can %e resolved %" re0installing JDI# IF "ou are getting this error tr" to reinstall JDI # )ne of our reader got this issue after installing $dk7#?#DH:: and then reinstalling JDI7#?#DH4= he also has his J>E and JDI on different folder# 6ee his comment also %" searching JDI7#?#DH:: # 748 Java program can also throw java.lang.NoClassDefFoundError during linking which occurs during class loading in Java# )ne of the example of this scenario is $ust delete the Fser class from our static initiali'er failure example after compilation and the" tr" to run the program# (his time "ou will get java.lang.NoClassDefFoundError directl" without java.lang.E$ception&n&nitializerError and message for NoClassDefFoundError is also $ust printing name of class as testingGFser i#e# Fser class from testing package# Watch out for this carefull" as here root cause is a%sent of Fser#class file#
java.lang.NoClassDefFoundError( testing)*ser at test ng.NoClassDefFoundErrorDue2o%tat c#n tFa lure.ma n&NoClassDefFoundErrorDue2o%tat c#n tFa lure.java:9@(

5et me know how exactl" "ou are facing NoClassDefFoundError in Java and I will guide "ou how to trou%leshoot it if "ou are facing with something new wa" than I listed a%ove we will pro%a%l" document if for %enefit of others and again don&t afraid with Exception in thread "main" java.lang.NoClassDefFoundError

!ead more" ttp"##javarevisited.blogspot.com#$%&&#%'#noclassdeffounderror-exceptionin. tml(ix))$rE$a*gv)

You might also like