Wednesday, December 11, 2019

Java.Lang.Noclassdeffounderror: Org/Dom4j/Documentexception [Solution]

Exception inwards thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException comes when your computer programme is using DOM4j library but necessary JAR is non present. This mistake tin too come upward when y'all are indirectly using DOM4j library e.g. when y'all purpose Apache POI library to read XLSX file inwards Java,  this library needs dom4j.jar inwards your classpath. Not but this i but at that spot are several other libraries which purpose this JAR internally, if y'all are using whatever of them but don't convey this JAR hence your computer programme volition compile fine but neglect at runtime because JVM volition endeavour to charge this bird but volition non last able to honour it on the classpath. Some curious developers mightiness ask, why it didn't neglect during compile fourth dimension if JAR was non acquaint there? Well, the argue for that is that your code mightiness non last using whatever bird file straight from the dom4j.jar file.

When y'all compile your computer programme e.g. ExcelReader.java, hence the compiler volition exclusively await for a bird file which is straight referenced or required to generate the bird file, it volition non await for transitive dependencies.

For example, suppose y'all demand WorkBook bird from Apache  POI, which internally uses dom4j library, compiler volition complain if poi.jar is non there, but volition non complain fifty-fifty if dom4j.jar is non acquaint because its compiling your code, it's non compiling WorkBook class, it's already compiled because bird file of this code is already acquaint inwards poi.jar. But things works differently when your run the program, at that fourth dimension JVM volition await for all the binary code.

When it volition charge WorkBook bird it volition endeavour to charge the referenced bird from DOM4j library as well as at that fourth dimension if JVM didn't honour those classes inwards CLASSPATH, it volition throw java.lang.NoClassDefFoundError: org/dom4j/DocumentException.

It tin too come upward every bit java.lang.ClassNotFoundException: org.dom4j.DocumentException but exclusively if your code tries to charge this bird explicitly instead of JVM. That's truly the difference betwixt ClassNotFoundException as well as NoClassDefFoundError inwards Java. Now 1000000 dollar question? How practise y'all solve this error? Just add together a dom4j-1.6.1.jar file into classpath.




Cause of java.lang.NoClassDefFoundError: org/dom4j/DocumentException

As I said the source drive of this mistake is missing dom4j JAR file. It could last due to guide dependency or 3rd political party dependency e.g. y'all are using Apache POI library to read as well as write Excel file inwards Java as well as that library demand dom4j JAR file but y'all convey exclusively include poi.jar into your classpath.  Here is how the stack draw of this mistake volition await similar :

Exception inwards thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
        at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:154)
        at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
        at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:54)
        at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:82)
        at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:228)
        at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:1
87)
        at ExcelReader.main(ExcelReader.java:12)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 8 more

Let's endeavour to sympathize this mistake stack-trace inwards detail. You tin come across that source drive of this mistake is  "java.lang.ClassNotFoundException: org.dom4j.DocumentException", which suggests farewell "Caused yesteryear :" line. You tin come across that JVM was trying to charge that class from the reference of "java.net.URLClassLoader". If y'all movement upward y'all tin too come across that this mistake was initiated from your code at ExcelReader.main(ExcelReader.java:12), which is the twelfth occupation of your ExcelReader.java source file, which is null but a unproblematic computer programme which read information from XLSX file inwards Java. If y'all cry back correctly, XLSX  is an Open XML format file as well as y'all demand the poi-ooxml-XX.jar file to read that file.  A bird called org.apache.poi.openxml4j.opc.OPCPackage needs org.dom4j.DocumentException as well as their JVM failed because it didn't works life org.dom4j.DocumentException bird because the JAR file which contains this bird file is non acquaint inwards CLASSPATH. 



How to solve java.lang.NoClassDefFoundError: org/dom4j/DocumentException

If y'all sympathize the drive of this work as well as solution is easy.  This exception tin last solved yesteryear adding dom4j-1.6.1.jar to the classpath. The exact version of JAR file depends on upon inwards your code or 3rd political party library which is using this JAR file. It may possible that adding arbitrary JAR may effect inwards another mistake or exception. Usually, this types of mistake come upward when y'all manually larn by your projection dependencies yesteryear downloading JAR files from the internet. I advise using Apache Maven to larn by projection dependencies because it too downloads transitive dependencies e.g. if y'all had used Maven to download poi-ooxml-XX.jar hence it would convey automatically downloaded the dom4j-XX.jar file. If at that spot is no selection as well as y'all convey to add together JAR files manually hence brand certain y'all read well-nigh 3rd political party dependencies of your library as well as download right version of JAR files. 

j library but necessary JAR is non acquaint java.lang.NoClassDefFoundError: org/dom4j/DocumentException [Solution]



That's all well-nigh how to solve java.lang.NoClassDefFoundError: org/dom4j/DocumentException mistake inwards Java. As y'all seen this mistake comes when your projection is using org.dom4j.DocumentException bird either straight or indirectly but the dom4j-1.6.1.jar file is non acquaint inwards CLASSPATH. In firm to solve java.lang.NoClassDefFoundError: org/dom4j/DocumentException just add together dom4j-1.6.1.jar file into your program's classpath. You tin too purpose the same solution to solve java.lang.ClassNotFoundException: org.dom4j.DocumentException mistake every bit well, because both are the just same exclusively divergence is that inwards the instance of old JVM initiated the procedure of loading a bird as well as inwards the instance of afterwards your code explicitly tried to charge the bird file. 

Further Learning
Complete Java Masterclass
answer)
  • Difference betwixt RuntimeException as well as checked exception inwards Java? (answer)
  • Difference betwixt throw as well as throws inwards Java? (answer)
  • How to practise user defined exception inwards Java? (answer)
  • How to fix java.lang.OutOfMemoryError: Direct buffer retentiveness inwards Java (solution)
  • How to solve java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject? (solution)
  • Could non practise the Java virtual machine Invalid maximum heap size: -Xmx (solution)
  • java.lang.unsupportedclassversionerror unsupported major.minor version 50.0? (solution)
  • How to bargain with org.springframework.web.context.ContextLoaderListener ? (solution)
  • How to solve java.lang.OutOfMemoryError: Java heap infinite inwards Tomcat as well as Eclipse? (solution)
  • How to fix  java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver inwards Java? (solution)

  • No comments:

    Post a Comment