Friday, November 22, 2019

Java- Exception Inwards Thread Master Copy Java.Lang.Noclassdeffounderror Movement Too Solution

NoClassDefFoundError inwards Java
The Exception inwards thread "main" java.lang.NoClassDefFoundError is a mutual mistake inwards Java which occurs if a ClassLoader is non able to uncovering a item shape inwards the classpath land trying to charge it. The Exception inwards thread "main" suggests that this mistake has occurred inwards the top dog thread, the thread which is responsible for running Java application. This mistake tin occur to whatsoever thread but if it happens inwards top dog thread in addition to so your plan volition crash. As per Javadoc, NoClassDefFoundError can locomote thrown during linking or loading of the shape file. It's denoted yesteryear java.lang.NoClassDefFoundError in addition to comes when that item shape is introduce during compile fourth dimension but somehow non available during runtime. This could locomote due to missing JAR file, whatsoever permission issue, or wrong classpath on runtime, depending upon your environment.


Java Programmers oft confuse betwixt java.lang.ClassNotFoundException and java.lang.NoClassDefFoundError in Java which is too related to shape loading inwards Java but at that topographic point is a slight deviation betwixt NoClassDefFoundError in addition to ClassNotFoundException inwards Java, The ClassNotFoundException comes when Java tries to charge a shape yesteryear its binary get upwards during runtime in addition to Class is non available at that time. for example, when you lot charge JDBC driver using Class.forName(String classname), you lot volition acquire java.lang.ClassNotFoundException instead of NoClassDefFoundError.

On the other hand, NoClassDefFoundError comes when the shape was available at compile fourth dimension but somehow non available at runtime e.g. if static initializer block of a shape throws an Exception during loading, the shape volition non locomote available for other's to use. If whatsoever shape examine to refer that shape in addition to so the classloader volition throw the java.lang.NoClassDefFoundError. If you lot like, you lot tin read to a greater extent than well-nigh their deviation inwards this article.




5 mutual causes of  Exception inwards thread "main" java.lang.NoClassDefFoundErro in Java

The Exception inwards thread "main" java.lang.NoClassDefFoundError inwards Java tin come upwards due to a multifariousness of reasons inwards Java, But most of the fourth dimension java.lang.NoClassDefFoundError comes due to CLASSPATH errors inwards Java. Influenza A virus subtype H5N1 right agreement of how classpath plant inwards Java is required to troubleshoot in addition to solve the  Exception inwards thread "main" java.lang.NoClassDefFoundError in Java.

Anyway hither are approximately of the mutual argue which has caused  Exception inwards thread "main" java.lang.NoClassDefFoundError in diverse Java application inwards past, your work could good locomote due to whatsoever of these reasons. 

1) Missing JAR file inwards Classpath
The offender shape is non available inwards the classpath of your Java application. For example, if you lot are getting "Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger" mistake inwards your application which agency classloader of your application is non able to uncovering org.apache.log4j.Logger shape inwards its classpath. The simplest reasons of this mistake are the missing log4j.jar file inwards the classpath. As before long every bit you lot add together that JAR into classpath the mistake goes away. See here to larn to a greater extent than well-nigh troubleshooting this item issue.

Similarly, you lot powerfulness convey seen java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject which comes if you lot are using XMLbeans but xmlbeans-2.60.jar is non introduce inwards your classpath (see here).


Another mutual 1 is java.lang.NoClassDefFoundError: org/dom4j/DocumentException, which comes due to missing dom4j.jar file (see here).

In short, the commencement measuring to solving "Exception inwards thread "main" java.lang.NoClassDefFoundError" is to banking concern tally if offender shape is available inwards classpath or not. In Eclipse, you lot tin purpose shortcuts similar search resources (Ctrl + R) or search type (Ctrl + T) to uncovering which JAR file that item shape belongs in addition to and so banking concern tally if that JAR is available inwards classpath or not. See here to larn to a greater extent than useful Eclipse shortcuts for Java developers.


2) Due to Visibility effect betwixt multiple Classloaders
Sometimes inwards complex environments e.g. Java EE container, it's possible that the offender cases is available inwards classpath but non visible to the classLoader which is trying to charge that item class. For example, inwards Java classloader run yesteryear delegation in addition to visibility. Influenza A virus subtype H5N1 shape loading asking is commencement delegated to bring upwards shape loader earlier shaver shape loader attempts to charge it, thence whatsoever shape loaded yesteryear bring upwards shape loader is visible to shaver shape loader but the reverse is non truthful i.e. a shape loaded yesteryear shaver classloader is non visible to bring upwards shape loader.

Suppose a shape is used yesteryear 2 JAR files e.g. WAR in addition to EJB-JAR file but it introduce inwards WAR file in addition to so it volition locomote visible to spider web app classloader but non create the classloader which loads the shape from EJB-JAR file.


3) Missing classpath entry inwards Manifest file
You tin too acquire the "Exception inwards thread "main" java.lang.NoClassDefFoundError" if you lot are running your Java application using java -jar ascendancy in addition to that shape were non defined inwards manifest file's ClassPath attribute. In past, I convey spent hours to figure out which JAR is missing solely to uncovering that Java is using the classpath defined inwards the manifest file in addition to non the classpath surroundings variable.

is a mutual mistake inwards Java which occurs if a Java- Exception inwards thread top dog java.lang.NoClassDefFoundError Cause in addition to Solution

4) Environment Issues 
I convey seen foreign reasons of java.lang.NoClassDefFound mistake land working inwards Java. In 1 instance 1 of the trounce scripts which was starting our Java application was overriding the value of here to larn to a greater extent than well-nigh how to add together multiople JAR files inwards classpath.



5) Error inwards Static Initializer block
One of the most mutual reasons for "Exception inwards thread "main" java.lang.NoClassDefFoundError" is mistake inwards static initializer block.  This is the block of code which is declared using static keyword in addition to executed when a shape is loaded. If suppose shape Influenza A virus subtype H5N1 throw Exception from static initializer block in addition to shape B uses shape A. In that instance when shape B volition locomote loaded it volition throw "Exception inwards thread "main" java.lang.NoClassDefFoundError" because it volition non locomote able to uncovering it's dependency which is shape A. 

So adjacent time, if you lot meet the NoClassDefFound mistake inwards your Java application, brand certain you lot search for static initializer mistake i.e. java.lang.ExceptionInInitializerError inwards your log file. That is the origin crusade of the NoClassDefFoundError you lot were seeing.


That's all on what is NoClassDefFoundError inwards Java in addition to what are the mutual argue which causes Exception inwards thread "main" java.lang.NoClassDefFoundError inwards Java. Most of the fourth dimension it's but a missing JAR file which causes this work but sometimes it tin locomote a nightmare to solve e.g. inwards instance your application is picking classpath from manifest file in addition to you lot are checking classpath entry somewhere else. In companionship to bargain alongside java.lang.NoClassDefFoundError, 2 things which are most of import is cognition of Classpath in addition to ClassLoader inwards Java. So, you lot should invest approximately fourth dimension on it. To start alongside you lot tin read Java Puzzlers yesteryear Joshua Bloch.


Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

No comments:

Post a Comment