3 Ways To Solve Java.Lang.Noclassdeffounderror Inward Coffee J2ee
I know how frustrating is to consider Exception inwards thread "main" java.lang.NoClassDefFoundError, which is a manifestation of NoClassDefFoundError inwards Java. I receive got seen it a yoke of times together with spent quite a lot fourth dimension initially to figure out what is wrong, which degree is missing etc. The outset error I did was mingling java.lang.ClassNotfoundException together with NoClassDefFoundError, inwards reality, they are totally different, together with my minute error was using trial together with error method to solve this java.lang.NoClassDefFoundError instead of agreement why NoClassDefFoundError is coming, what is the existent argue behind NoClassDefFoundError together with how to resolve this. In this Java tutorial, I receive got tried to rectify that mistakes together with uncover some secrets of NoClassDefFoundError inwards Java together with volition portion my sense to a greater extent than or less it. NoClassDefFoundError is non something which cannot hold upwards resolved or difficult to resolve it’s just its manifestation which puzzles nigh of Java developer. This is the nigh mutual error inwards Java evolution along alongside java.lang.OutOfMemoroyError: Java heap space together with java.lang.OutOfMemoryError: PermGen space Anyway lets’s consider Why NoClassDefFoundError comes inwards Java together with what to produce to resolve NoClassDefFoundError inwards Java.
What is the argue of NoClassDefFoundError inwards Java?
NoClassDefFoundError inwards Java comes when Java Virtual Machine is non able to discovery a especial degree at runtime which was available at compile time. For example, if nosotros receive got a method telephone telephone from a degree or accessing whatsoever static fellow member of a Class together with that degree is non available during run-time together with thence JVM volition throw NoClassDefFoundError. It’s of import to empathize that this is dissimilar than ClassNotFoundException which comes spell trying to charge a degree at run-time exclusively together with the holler was provided during runtime, non at compile-time. Many Java developer mingles this ii Error together with gets confused.
In short, NoClassDefFoundError volition come upwards if a degree was acquaint during compile fourth dimension but non available inwards coffee classpath during runtime. Normally you lot volition consider below draw of piece of job inwards log when you lot instruct NoClassDefFoundError:
Exception inwards thread "main" java.lang.NoClassDefFoundError
Exception inwards thread “main” but betoken that its “main” thread which is non able to discovery a especial degree it could hold upwards whatsoever thread thence just don’t worry. The deviation betwixt this error coming inwards the primary thread together with some other thread is when Exception inwards thread “main” comes computer programme crashes or near itself downward every bit opposed to other thread inwards which illustration your computer programme volition proceed to run. If you lot are actually curious together with retrieve that you lot empathize how degree loading works, I advise you lot sweat some puzzles from Joshua Bloch's Java Puzzlers, it has got some actually tricky questions to examine your knowledge.
The deviation betwixt java.lang.NoClassDefFoundError together with ClassNotFoundException inwards Java
Many times nosotros confused ourselves alongside java.lang.ClassNotFoundException together with java.lang.NoClassDefFoundError, though both of them related to Java Classpath they are completely dissimilar to each other. ClassNotFoundException comes when JVM tries to the charge a degree at runtime dynamically agency you lot plough over the holler of the degree at runtime together with and thence JVM tries to charge it together with if that degree is non works life inwards the classpath it throws java.lang.ClassNotFoundException. While inwards the illustration of NoClassDefFoundError the problematic degree was acquaint during Compile fourth dimension together with that's why the computer programme successfully compiled but non available during runtime for whatsoever reason. NoClassDefFoundError is easier to solve than ClassNotFoundException in my sentiment because hither nosotros know that Class was acquaint at create time, but it totally depends upon surround if you lot are working inwards the J2EE surround than you lot tin dismiss instruct NoClassDefFoundError fifty-fifty if the degree is acquaint because it may non hold upwards visible to the corresponding degree loader. See my postal service NoClassDefFoundError vs ClassNotFoundException inwards Java for to a greater extent than details.
How to resolve java.lang.NoClassDefFoundError inwards Java
The obvious argue of NoClassDefFoundError is that a especial degree is non available inwards Classpath, thence nosotros ask to add together that into Classpath or nosotros ask to banking concern check why it’s non available inwards Classpath if nosotros are expecting it to be. There could hold upwards multiple reasons like:
2) You mightiness hold upwards running your computer programme using offend ascendency together with degree was non defined inwards manifest file's ClassPath attribute.
3) Any start-up script is overriding Classpath surround variable.
4) Because NoClassDefFoundError is a subclass of java.lang.LinkageError it tin dismiss also come upwards if ane of it dependency similar native library may non available.
4) Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to the failure of static initialization is quite common.
5) If you lot are working inwards J2EE surround than the visibility of Class amid multiple Classloader can also drive java.lang.NoClassDefFoundError, consider examples together with scenario department for detailed discussion.
We volition at nowadays consider a yoke of examples together with scenarios when java.lang.NoClassDefFoundError has come upwards earlier together with how it's been resolved. This tin dismiss assist you lot to troubleshoot the root drive of NoClassDefFoundError inwards Java application.
NoClassDefFoundError inwards Java - Example together with Scenarios
1. H5N1 uncomplicated illustration of NoClassDefFoundError is degree belongs to a missing JAR file or JAR was non added into classpath or sometimes jar's holler has been changed past times soul similar inwards my illustration ane of my colleagues has changed tibco.jar into tibco_v3.jar together with the program is failing alongside java.lang.NoClassDefFoundError and I were wondering what's wrong.
2. The degree is non inwards Classpath, at that spot is no certain shot way of knowing it but many times you lot tin dismiss just receive got a human face to impress System.getproperty("java.classpath") and it volition impress the classpath from at that spot you lot tin dismiss at to the lowest degree instruct an thought of your actual runtime classpath.
3. Just sweat to run alongside explicitly -classpath selection alongside the classpath you lot retrieve volition move together with if it's working together with thence it's a certain brusk sign that soul is overriding coffee classpath.
NoClassDefFoundError inwards Java due to Exception inwards Static Initializer block
This is some other mutual argue of java.lang.NoClassDefFoundError, when your degree performs some static initialization inwards a static block similar many Singleton classes initialized itself on the static block to receive got payoff of thread-safety provided past times JVM during the degree initialization process, together with if static block throws an Exception, the degree which is referring to this degree volition instruct NoclassDefFoundError inwards Java. If you lot human face at your log file you lot should picket for whatsoever java.lang.ExceptionInInitializerError because that could trigger java.lang.NoClassDefFoundError: Could non initialize class on other places. Like inwards below code example, During degree loading together with initialization User degree are throwing Exception from static initializer block, which trigger ExceptionInInitializerError during outset fourth dimension loading of User degree inwards reply to novel User() call. Later residue of novel User() are failing every bit java.lang.NoClassDefFoundError. the province of affairs gets worst if master ExceptionInInitializerError, which is root drive hither is silently eaten past times whatsoever code.
Code Example of NoClassDefFoundError due to Static block Exception:
/**
* Java computer programme to demonstrate how failure of static initialization later on cause
* java.lang.NoClassDefFoundError inwards Java.
* @author Javin Paul
*/
public class NoClassDefFoundErrorDueToStaticInitFailure {
public static void main(String args[]){
List<User> users = new ArrayList<User>(2);
for(int i=0; i<2; i++){
try{
users.add(new User(String.valueOf(i))); //will throw NoClassDefFoundError
}catch(Throwable t){
t.printStackTrace();
}
}
}
}
class User{
private static String USER_ID = getUserId();
public User(String id){
this.USER_ID = id;
}
private static String getUserId() {
throw new RuntimeException("UserId Not found");
}
}
* Java computer programme to demonstrate how failure of static initialization later on cause
* java.lang.NoClassDefFoundError inwards Java.
* @author Javin Paul
*/
public class NoClassDefFoundErrorDueToStaticInitFailure {
public static void main(String args[]){
List<User> users = new ArrayList<User>(2);
for(int i=0; i<2; i++){
try{
users.add(new User(String.valueOf(i))); //will throw NoClassDefFoundError
}catch(Throwable t){
t.printStackTrace();
}
}
}
}
class User{
private static String USER_ID = getUserId();
public User(String id){
this.USER_ID = id;
}
private static String getUserId() {
throw new RuntimeException("UserId Not found");
}
}
Output
java.lang.ExceptionInInitializerError
at testing.NoClassDefFoundErrorDueToStaticInitFailure.main(NoClassDefFoundErrorDueToStaticInitFailure.java:23)
Caused by: java.lang.RuntimeException: UserId Not found
at testing.User.getUserId(NoClassDefFoundErrorDueToStaticInitFailure.java:41)
at testing.User.<clinit>(NoClassDefFoundErrorDueToStaticInitFailure.java:35)
... 1 more
java.lang.NoClassDefFoundError: Could non initialize class testing.User
at testing.NoClassDefFoundErrorDueToStaticInitFailure.main(NoClassDefFoundErrorDueToStaticInitFailure.java:23)
java.lang.ExceptionInInitializerError
at testing.NoClassDefFoundErrorDueToStaticInitFailure.main(NoClassDefFoundErrorDueToStaticInitFailure.java:23)
Caused by: java.lang.RuntimeException: UserId Not found
at testing.User.getUserId(NoClassDefFoundErrorDueToStaticInitFailure.java:41)
at testing.User.<clinit>(NoClassDefFoundErrorDueToStaticInitFailure.java:35)
... 1 more
java.lang.NoClassDefFoundError: Could non initialize class testing.User
at testing.NoClassDefFoundErrorDueToStaticInitFailure.main(NoClassDefFoundErrorDueToStaticInitFailure.java:23)
5) Since NoClassDefFoundError is an also a LinkageError which arises due to dependency on some other class, you lot tin dismiss also instruct java.lang.NoClassDefFoundError if your computer programme is theme on native library together with the corresponding DLL is non there. Remember this tin dismiss also trigger java.lang.UnsatisfiedLinkError: no dll inwards java.library.path Exception Java. In guild to solve this maintain your dll along alongside JAR.
6) If you lot are using ANT create file create JAR together with manifest file together with thence its worth noting to debug till that bird to ensure that ANT build script is getting the right value of classpath together with appending it to manifest.mf file.
7) Permission number on JAR file tin dismiss also drive NoClassDefFoundError inwards Java. If you lot are running your Java computer programme inwards a multi-user operating organisation similar Linux together with thence you lot should hold upwards using application user id for all your application resources similar JAR files, libraries, together with configuration. If you lot are using shared library which is shared amid multiple application which runs nether dissimilar users then you lot may come across permission issue, similar JAR file is owned past times some other user together with non accessible to your application. One of our reader “it’s me said”, faced java.lang.NoClassDefFoundError due to this reason. See his comment also.
8) Typo on XML Configuration tin dismiss also drive NoClassDefFoundError inwards Java. As nigh of Java frameworks similar Spring, Struts they all purpose XML configuration for specifying beans. By whatsoever chance, if you lot set the edible bean holler wrong, it may surface every bit java.lang.NoClassDefFoundError while loading other degree which has a dependency on wrongly named bean. This is quite mutual inwards Spring MVC framework together with Apache Struts where you lot instruct tons of Exception inwards thread "main" java.lang.NoClassDefFoundError, while deploying your WAR or EAR file.
9) Another illustration of java.lang.NoClassDefFoundError, every bit mentioned past times our reader Nick, is that when your compiled degree which is defined inwards a package, doesn’t acquaint inwards the same parcel spell loading similar inwards the illustration of JApplet it volition throw NoClassDefFoundError inwards Java. Also, consider Nick’s comment on this error.
10) java.lang.NoClassDefFoundError can hold upwards caused due to multiple classloaders inwards J2EE environments. Since J2EE doesn’t advert measure classloader construction together with it depends on dissimilar vendors similar Tomcat, WebLogic, WebSphere on how they charge dissimilar components of J2EE similar WAR file or EJB-JAR file. In guild to troubleshoot NoClassDefFoundError inwards J2EE application cognition of How ClassLoader works inwards Java is mandatory. Just to recap ClasLoader works on 3 regulation delegation, visibility, together with uniqueness. Delegation agency every asking to charge a degree is delegated to bring upwards classloader, visibility agency an powerfulness to works life classes loaded past times the classloader, all tike classloader tin dismiss consider classes loaded past times bring upwards classloader, but bring upwards classloader tin dismiss non consider the degree loaded past times tike classloaders. Uniqueness enforce that degree loaded past times the bring upwards volition never hold upwards reloaded past times tike classloaders. Now suppose if a degree say User is acquaint inwards both WAR file together with EJB-JAR file together with loaded past times WAR classloader which is tike classloader which loads the degree from EJB-JAR. When a code inwards EJB-JAR refers to this User class, Classloader which loaded all EJB degree doesn’t works life that because it was loaded past times WAR classloader which is a tike of it.
This volition upshot inwards java.lang.NoClassDefFoundError for User class. Also, If the degree is acquaint inwards both JAR file together with you lot volition telephone telephone equals method to compare those ii object, it volition upshot inwards ClassCastException every bit object loaded past times ii dissimilar classloaders tin dismiss non hold upwards equal.
This volition upshot inwards java.lang.NoClassDefFoundError for User class. Also, If the degree is acquaint inwards both JAR file together with you lot volition telephone telephone equals method to compare those ii object, it volition upshot inwards ClassCastException every bit object loaded past times ii dissimilar classloaders tin dismiss non hold upwards equal.
11) Some of the readers of this weblog also suggested that they instruct Exception inwards thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac/Main , this error agency either your Classpath, PATH or JAVA_HOME is non setup properly or JDK installation is non correct. which tin dismiss hold upwards resolved past times reinstalling JDK? IF you lot are getting this error sweat to reinstall JDK. One of our readers got this number after installing jdk1.6.0_33 together with and thence reinstalling JDK1.6.0_25, he also has his JRE together with JDK inwards a dissimilar folder. See his comment also past times searching JDK1.6.0_33.
12) Java computer programme tin dismiss also throw java.lang.NoClassDefFoundError during linking which occurs during degree loading inwards Java. One of the examples of this scenario is just delete the User degree from our static initializer failure illustration after compilation together with they sweat to run the program. This time, you lot volition instruct java.lang.NoClassDefFoundError directly without java.lang.ExceptionInInitializerError and message for NoClassDefFoundError are also just printing the holler of the degree every bit testing/User i.e. User degree from the testing package. Watch out for this carefully every bit hither root drive is absent of User.class file.
java.lang.NoClassDefFoundError: testing/User
at testing.NoClassDefFoundErrorDueToStaticInitFailure.main(NoClassDefFoundErrorDueToStaticInitFailure.java:23)
at testing.NoClassDefFoundErrorDueToStaticInitFailure.main(NoClassDefFoundErrorDueToStaticInitFailure.java:23)
Let me know how just you lot are facing NoClassDefFoundError inwards Java and I volition guide you lot how to troubleshoot it if you lot are facing alongside something novel way than I listed inwards a higher house nosotros volition likely document if for the produce goodness of others together with in ane lawsuit to a greater extent than don’t afraid alongside Exception inwards thread "main" java.lang.NoClassDefFoundError.
Further Learning
Complete Java Masterclass
How to solve UnSupportedClassVersionError inwards Java
Belum ada Komentar untuk "3 Ways To Solve Java.Lang.Noclassdeffounderror Inward Coffee J2ee"
Posting Komentar