Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache POI ClassNotFoundException

I am using eclipse to try and get a list of sheets from an excel spreadsheet but when I go to run it Java throws exceptions.

here is the code;

File myFile = new File("excel.xlsx");
    Workbook wb = null;
    try {
        wb = WorkbookFactory.create(myFile);
    } catch (EncryptedDocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    List<String> sheetNames = new ArrayList<String>();
    for (int i=0; i<wb.getNumberOfSheets(); i++) {
        sheetNames.add( wb.getSheetName(i) );
    }
    System.out.println(sheetNames);

here is the log;

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)
    at com.cogentautomation.view.View.main(View.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
    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)
    ... 4 more

I have included the following in the eclipse java build path as libraries;

  • commons-codec
  • commons-logging
  • junit
  • log4j
  • poi
  • poi-ooxml
  • poi-scratchpad

I know the solution is something simple but my mind is blanking. I am not using maven.

like image 876
glend Avatar asked Sep 08 '26 13:09

glend


2 Answers

You will need to add XMLBeans dependency.You can include the dependency from here https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/2.6.0

like image 133
Ramachandran.A.G Avatar answered Sep 10 '26 01:09

Ramachandran.A.G


As the exception message is telling you, you need the XMLBeans library. Poi probably depends on it.

If you were using a dependency management tool such as Gradle or Maven, it would have automatically been included.

like image 21
E-Riz Avatar answered Sep 10 '26 03:09

E-Riz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!