47

I am getting following error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject at OrderBook.WriteToExcelSheet.CreateOutPutFile(WriteToExcelSheet.java:20) at OrderBook.MainMethod.main(MainMethod.java:71)

I looked for the reasons for this error online but couldn't find why I am getting it.

I have included the following jar files

poi-3.9-20121203.jar, 
poi-excelant-3.9-20121203.jar,
poi-examples-3.9-20121203.jar,
poi-ooxml-3.9-20121203.jar,
poi-ooxml-schemas-3.9-20121203.jar,
poi-scratchpad-3.9-20121203.jar

Code:

public class WriteToExcelSheet {
    public static Map < Integer, Object[] > data = new TreeMap < Integer, Object[] > ();
    public static void CreateOutPutFile() {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Orderbook Stats");
        //This data needs to be written (Object[])
        //Iterate over data and write to sheet
        Set < Integer > keyset = data.keySet()
        int rownum = 0;
        for (Integer key: keyset) {
            Row row = sheet.createRow(rownum++);
            Object[] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj: objArr) {
                Cell cell = row.createCell(cellnum++);
                if (obj instanceof String) cell.setCellValue((String) obj);
                else if (obj instanceof Integer) cell.setCellValue((Integer) obj);
            }
        }
        try {
            //Write the workbook in file system
            System.out.println("OutPutStats.xlsx writing..............................");
            FileOutputStream out = new FileOutputStream(new File("FileLocation/o.xlxs"));
            workbook.write(out);
            out.close();
            System.out.println("OutPutStats.xlsx written successfully on disk.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}    
Sachin
  • 40,216
  • 7
  • 90
  • 102
user2942227
  • 1,023
  • 6
  • 19
  • 26

9 Answers9

99

You have to include one more jar.

xmlbeans-2.3.0.jar

Add this and try.

Note: It is required for the files with .xlsx formats only, not for just .xls formats.

Jhanvi
  • 5,069
  • 8
  • 32
  • 41
  • 2
    @user2942227 you can download it from [here](http://www.java2s.com/Code/Jar/x/Downloadxmlbeans230jar.htm) – Jhanvi Nov 02 '13 at 04:52
  • Now it threw this error,Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentExceptionCaused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException, Do you know why? – user2942227 Nov 02 '13 at 04:57
  • 1
    @user2942227 add dom4j-1.6.1.jar , download it from [here](http://www.java2s.com/Code/Jar/d/Downloaddom4j16jar.htm) – Jhanvi Nov 02 '13 at 04:58
  • 1
    Look for an folder named `ooxml-lib` in the POI archive that you downloaded. – Tejas Kale Sep 15 '14 at 11:41
  • i had added both jars then also its giving same error – asiya Sep 19 '14 at 10:38
  • 1
    @asiya can you list all the jars that you added?? – Jhanvi Sep 19 '14 at 11:15
  • thanks..i resolved it..i was running jar file instead of project – asiya Sep 19 '14 at 12:01
  • I just want to add that the Jars are included with the POI Jars. Please take a look at this questions: http://stackoverflow.com/questions/13046953/exception-while-reading-excel-file-with-apache-poi/13047044#13047044 It helped me so much! – Sahil Gupta Apr 22 '16 at 15:38
  • Thank you :-). It gave me an exception for org/apache/commons/collections4/ListValuedMap which was resolved by adding common-collections4-4.1.jar – Vishal Kotak Apr 13 '17 at 14:30
4

you have to include two more jar files.

xmlbeans-2.3.0.jar and dom4j-1.6.1.jar Add try it will work.

Note: It is required for the files with .xlsx formats only, not for just .xlt formats.

2

When trying to translate Excel file with .xlsx suffix, you need to add additional jar, xmlbeansxxx.jar. xxxx is version, such as xmlbeans-2.3.0.jar

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Mengjun
  • 3,159
  • 1
  • 15
  • 21
  • It fixed that error. Now, it threwNow it threw this error,Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentExceptionCaused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException, Do you know why? – user2942227 Nov 02 '13 at 04:58
  • 1
    You need to add dom4j.jar as well. Since some XML file in POI uses dom4j to handle XML file. – Mengjun Nov 02 '13 at 04:59
  • When you downloads src or bin for POI, there should be a folder named lib. all of the dependency libs, such as xmlBeans and dom4j. jar are thers. – Mengjun Nov 02 '13 at 05:03
1

For all that you add xmlbeans-2.3.0.jar and it is not working,you must use HSSFWorkbook instead of XSSFWorkbook after add jar.For instance;

    Workbook workbook = new HSSFWorkbook();
    Sheet listSheet = workbook.createSheet("Kişi Listesi");

    int rowIndex = 0;
    for (KayitParam kp : kayitList) {
        Row row = listSheet.createRow(rowIndex++);
        int cellIndex = 0;
        row.createCell(cellIndex++).setCellValue(kp.getAd());
        row.createCell(cellIndex++).setCellValue(kp.getSoyad());
        row.createCell(cellIndex++).setCellValue(kp.getEposta());
        row.createCell(cellIndex++).setCellValue(kp.getCinsiyet());
        row.createCell(cellIndex++).setCellValue(kp.getDogumtarihi());
        row.createCell(cellIndex++).setCellValue(kp.getTahsil());
    }

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);
        AMedia amedia = new AMedia("Kisiler.xls", "xls",
                "application/file", baos.toByteArray());
        Filedownload.save(amedia);
        baos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
emreturka
  • 846
  • 3
  • 18
  • 44
1

You need to include xmlbeans-xxx.jar and if you have downloaded the POI binary zip, you will get the xmlbeans-xxx.jar in ooxml-lib folder (eg: \poi-3.11\ooxml-lib)

This jar is used for XML binding which is applicable for .xlsx files.

Amit Baderia
  • 4,454
  • 3
  • 27
  • 19
1

I faced a similar situation, so i replaced all the external jar files(poi-bin-3.17-20170915) and make sure you add other jar files present in lib and ooxml-lib folders.

Hope this helps!!!:)

eduPeeth
  • 1,840
  • 13
  • 21
Rashmi A
  • 11
  • 1
0

I was working with talend V7.3.1 and I had poi version "4.1.0" and including xml-beans from the list of dependencies didnt fix my problem (i.e: 2.3.0 and 2.6.0).

It was fixed by downloading the jar "xmlbeans-3.0.1.jar" and adding it to the project

enter image description here

Rabhi salim
  • 486
  • 5
  • 17
0

If to do this right way, you need to create a maven project and put all depenencies into pom.xml file, dependencies for maven you can google, for example: https://mvnrepository.com/artifact/org.apache.poi/poi/5.0.0?

If to do this hardcore way, download POI library binary files and add all the .jar files from all of the folders (for POI 5.0.0) this is (auxiliary, ooxml-lib, lib) folders and root catalog. In my case it works, if you don't want to figure out what library relate for what.

Something like that:

enter image description here

Ethan
  • 876
  • 8
  • 18
  • 34
-1

ClassNotFoundException is thrown when the class is not found in the classpath. Adding the following jar(contains the definition of the XmlObject interface) will solve the problem

xmlbeans-x.y.z.jar

You can download the latest xmlbeans jar file at the following link https://xmlbeans.apache.org/download/index.html

If you are working with the apache poi library make sure you add jars in the lib and ooxml-lib folders.

king.reflex
  • 313
  • 4
  • 10
  • @TammaliDeppak the accepted answers 7 years ago was to add a single specific missing JAR - adding all the JARs while it might work doesn't seem to add new thought to the problem. Please feel free to update your answer with a new insight. – Mr R Mar 27 '21 at 12:27
  • @MrR I had the same problem while I was working with the apache poi library. I found that that I did not add the jar files that are present in the lib and ooxml-lib folders. Adding all dependency jars helped. Though as you said I should have given a thought while writing the post, It didn't convey the right information. Thanks :). – king.reflex Mar 29 '21 at 03:08