Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException comes when your program is using DOM4j library but necessary JAR is not present. This error can also come when you are indirectly using DOM4j library e.g. when you use Apache POI library to read XLSX file in Java, this library need dom4j.jar in your classpath. Not just this one but there are several other libraries which uses this JAR internally, if you are using any of them but don't have this JAR then your program will compile fine but fail at runtime because JVM will try to load this class but will not be able to find it on classpath. Some curious developers might ask, why it didn't fail during compile time, if JAR was not present there? Well reason for that is that your code might not be using any class file directly from the dom4j.jar file. When you compile your program e.g. ExcelReader.java, then compiler will only look for class file which is directly referenced or required to generate class file, it will not look for transitive dependencies. For example, suppose you need WorkBook class from Apache POI, which internally uses dom4j library, compiler will complain if poi.jar is not there, but will not complain even if dom4j.jar is not present because its compiling your code, its not compiling WorkBook class, its already compiled because class file of this code is already present in poi.jar. But things works differently when your run the program, at that time JVM will look for all the binary code. When it will load WorkBook class it will try to load the referenced class from DOM4j library and at that time if JVM didn't find those classes in CLASSPATH, it will throw java.lang.NoClassDefFoundError: org/dom4j/DocumentException. It can also come as java.lang.ClassNotFoundException: org.dom4j.DocumentException but only if your code tries to load this class explicitly instead of JVM. That's actually the difference between ClassNotFoundException and NoClassDefFoundError in Java. Now million dollar question? How do you solve this error? Just add dom4j-1.6.1.jar file into classpath.
Read more »
No comments:
Post a Comment