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.
Saturday, June 27, 2015
Friday, June 26, 2015
You can read a text file in Java 6 by using BufferedReader or Scanner class. Both class provides convenient methods to read a text file line by line e.g. Scanner provides nextLine() method and BufferedReader provides readLine() method. If you are reading a binary file, you can use use FileInputStream. By the way, when you are reading text data, you also need to provide character encoding, if you don't then platform's default character encoding is used. In Java IO, streams like InputStream are used to read bytes and Readers like FileReader are used to read character data. BufferedReader is the traditional way to read data because it reads file buffer by buffer instead of character by character, so its more efficient if you are reading large files. BufferedReader is also there from JDK 1 itself, while Scanner was added on Java 5. Scanner has more features than BufferedReader, when it comes to file reading, for example you can specify any delimiter instead of new line, which is not possible with BufferedReader. Java 7 added new File API, which makes it reading/writing from file even more easier. Its's possible to read entire file in one line in Java 7, but given most of the projects are still running on Java 6, its good to know about these two ways to read a text file in Java. For Java beginners, I also suggest to refer a good book like Cay S. Horstmann, Core Java Volume 1 and 2 to learn basics of Java programming.
Tuesday, June 23, 2015
In this tutorial, you will learn how to check if a string is a palindrome in Java using recursion. A String is nothing but collection of characters e.g. "Java" and String literals are encoded in double quotes in Java. A String is said to be palindrome if reverse of String is equal to itself e.g. "aba" is a palindrome because reverse of "aba" is also "aba", but "abc" is not a palindrome because reverse of "abc" is "cba" which is not equal. Recursion means solving a problem by writing a function which calls itself. In order to check if String is palindrome in Java, we need a function which can reverse the String. Once you have original and reversed String, all you need to do is check if they are equal to each other or not. If they are equal then String is palindrome or not. You can write this reverse() function by using either for loop or by using recursion. If you remember, I already shared logic of reversing String in my earlier post, how to reverse String in Java using Iteration and recursion. Here we will use the same logic to check if String is palindrome or not. By the way, if you are preparing for coding interviews and looking for some coding problem to get hands on practice, I suggest you to take a look at Cracking the Coding Interview: 150 Programming Questions and Solutions. This is a wonderful book, which contains lots of easy and medium difficulty level coding problems, which will not only help you to prepare for interview but also develop your programming logic.