Skip to content
Advertisement

Unable to open XLSX file using Apache POI: NoClassDefFoundError

I’m having problems trying to open an XLSX file with Apache POI.

My code:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelTest {
    public static void main(String[] args) {
        try(FileInputStream f = new FileInputStream(new File("path/to/my/file.xlsx"))) {
            XSSFWorkbook wb = new XSSFWorkbook(f); // This is the line throwing the exception
        } catch(IOException e) {
            System.err.println(e.getMessage());
            e.printStackTrace(System.err);
        }

    }
}

Exception thrown:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
    at my.TEST.ExcelTest.main(ExcelTest.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

Background:

  • Running JDK 7 on MacOS X (Mavericks)
  • Using NetBeans
  • Imported jars (all from Apache POI binary distribution):
    • poi-3.11-20141221.jar
    • poi-ooxml-3.11-20141221.jar
    • poi-ooxml-schemas-3.11-20141221.jar
    • commons-codec-1.9.jar
    • log4j-1.2.17.jar

I can’t even start doing the real stuff, since I can’t even open the book! 🙁

When I saw the exception, I thought “Ok, let’s get xmlbeans and see if it works”, but xmlbeans is moved into Apache’s attic.

Am I missing something? How can I open the workbook?

Advertisement

Answer

I think you were right about needing xmlbeans-2.6.0.jar as well. That appears to be one of Poi’s dependencies. I set up a vestigial Maven project with poi, poi-scratchpad, poi-ooxml, and poi-ooxml-schemas as my direct dependencies. Maven yanked in these other JARs as dependencies:

  • commons-codec-1.9.jar
  • xmlbeans-2.6.0.jar
  • stax-api-1.0.1.jar

On further investigation, I see that stax-api is a transitive dependency for xmlbeans.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement