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 upwards when you lot are indirectly using DOM4j library e.g. when you lot role Apache POI library to read XLSX file inwards Java,  this library needs dom4j.jar inwards your classpath. Not but this ane but at that topographic point are several other libraries which role this JAR internally, if you lot are using whatever of them but don't accept this JAR in addition to then your computer programme volition compile fine but neglect at runtime because JVM volition effort to charge this shape but volition non hold out able to uncovering 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 hold out using whatever shape file straight from the dom4j.jar file.

When you lot compile your computer programme e.g. ExcelReader.java, in addition to then the compiler volition solely await for a shape file which is straight referenced or required to generate the shape file, it volition non await for transitive dependencies.

For example, suppose you lot demand WorkBook shape 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 shape file of this code is already acquaint inwards poi.jar. But things industrial plant differently when your run the program, at that fourth dimension JVM volition await for all the binary code.

When it volition charge WorkBook shape it volition effort to charge the referenced shape from DOM4j library in addition to at that fourth dimension if JVM didn't uncovering those classes inwards CLASSPATH, it volition throw java.lang.NoClassDefFoundError: org/dom4j/DocumentException.

It tin too come upwards equally java.lang.ClassNotFoundException: org.dom4j.DocumentException but solely if your code tries to charge this shape explicitly instead of JVM. That's genuinely the difference betwixt ClassNotFoundException in addition to NoClassDefFoundError inwards Java. Now ane grand m dollar question? How exercise you lot 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 rootage drive of this mistake is missing dom4j JAR file. It could hold out due to guide dependency or 3rd political party dependency e.g. you lot are using Apache POI library to read in addition to write Excel file inwards Java in addition to that library demand dom4j JAR file but you lot accept solely 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 effort to empathize this mistake stack-trace inwards detail. You tin run across that rootage drive of this mistake is  "java.lang.ClassNotFoundException: org.dom4j.DocumentException", which suggests good daytime "Caused past times :" line. You tin run across that JVM was trying to charge that class from the reference of "java.net.URLClassLoader". If you lot motion upwards you lot tin too run across that this mistake was initiated from your code at ExcelReader.main(ExcelReader.java:12), which is the twelfth business of your ExcelReader.java source file, which is nada but a uncomplicated computer programme which read information from XLSX file inwards Java. If you lot yell back correctly, XLSX  is an Open XML format file in addition to you lot demand the poi-ooxml-XX.jar file to read that file.  A shape called org.apache.poi.openxml4j.opc.OPCPackage needs org.dom4j.DocumentException in addition to their JVM failed because it didn't flora org.dom4j.DocumentException shape because the JAR file which contains this shape file is non acquaint inwards CLASSPATH. 



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

If you lot empathize the drive of this work in addition to solution is easy.  This exception tin hold out solved past times 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 lawsuit inwards another mistake or exception. Usually, this types of mistake come upwards when you lot manually larn by your projection dependencies past times 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 you lot had used Maven to download poi-ooxml-XX.jar in addition to then it would accept automatically downloaded the dom4j-XX.jar file. If at that topographic point is no selection in addition to you lot accept to add together JAR files manually in addition to then brand certain you lot read virtually 3rd political party dependencies of your library in addition to download right version of JAR files. 

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



That's all virtually how to solve java.lang.NoClassDefFoundError: org/dom4j/DocumentException mistake inwards Java. As you lot seen this mistake comes when your projection is using org.dom4j.DocumentException shape either straight or indirectly but the dom4j-1.6.1.jar file is non acquaint inwards CLASSPATH. In gild 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 role the same solution to solve java.lang.ClassNotFoundException: org.dom4j.DocumentException mistake equally well, because both are the just same solely departure is that inwards the representative of one-time JVM initiated the procedure of loading a shape in addition to inwards the representative of afterwards your code explicitly tried to charge the shape file. 


Java Troubleshooting Tutorials

Other Java mistake in addition to exception troubleshooting tutorials from this weblog :
  • What is departure betwixt Error in addition to Exception inwards Java? (answer)
  • Difference betwixt RuntimeException in addition to checked exception inwards Java? (answer)
  • Difference betwixt throw in addition to throws inwards Java? (answer)
  • How to exercise user defined exception inwards Java? (answer)
  • How to fix java.lang.OutOfMemoryError: Direct buffer retentivity inwards Java (solution)
  • How to solve java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject? (solution)
  • Could non exercise 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 in addition to Eclipse? (solution)
  • How to fix  java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver inwards Java? (solution)

Subscribe to receive free email updates:

0 Response to "java.lang.NoClassDefFoundError: org/dom4j/DocumentException [Solution]"

Posting Komentar