线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / xmlbeans / XmlException

时间:2010-04-20 07:10:23

标签: java

我必须在java中读取xls文件。我使用poi-3.6来读取Eclipse中的xls文件。但是我得到这个错误“线程中的异常”主“java.lang.NoClassDefFoundError:org / apache / xmlbeans ReadExcel2.main中的/ XmlException(ReadExcel2.java:38)“。

我添加了以下罐子 1)POI-3.6-20091214.jar 2)POI-的contrib-3.6-20091214.jar 3)POI-例子-3.6-20091214.jar 4)POI-OOXML-3.6-20091214.jar 5)POI-OOXML-架构 - 3.6-20091214.jar 6)POI暂存器-3.6-20091214.jar

以下是我使用的代码:

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class ReadExcel {

    public static void main(String[] args) throws Exception {
        //
        // An excel file name. You can create a file name with a full path
        // information.
        //
        String filename = "C:\\myExcel.xl";
        //
        // Create an ArrayList to store the data read from excel sheet.
        //
        List sheetData = new ArrayList();

        FileInputStream fis = null;
        try {
            //
            // Create a FileInputStream that will be use to read the excel file.
            //
            fis = new FileInputStream(filename);

            //
            // Create an excel workbook from the file system.
            //
           // HSSFWorkbook workbook = new HSSFWorkbook(fis);
            Workbook workbook = new XSSFWorkbook(fis);  

            //
            // Get the first sheet on the workbook.
            //
            Sheet sheet = workbook.getSheetAt(0);

            //
            // When we have a sheet object in hand we can iterator on each
            // sheet's rows and on each row's cells. We store the data read
            // on an ArrayList so that we can printed the content of the excel
            // to the console.
            //
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                Row row = (XSSFRow) rows.next();
                Iterator cells = row.cellIterator();

                List data = new ArrayList();
                while (cells.hasNext()) {
                    Cell cell = (XSSFCell) cells.next();
                    data.add(cell);
                }

                sheetData.add(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }

        showExelData(sheetData);
    }
    private static void showExelData(List sheetData) {
        //
        // Iterates the data and print it out to the console.
        //
        for (int i = 0; i < sheetData.size(); i++) {
            List list = (List) sheetData.get(i);
            for (int j = 0; j < list.size(); j++) {
                Cell cell = (XSSFCell) list.get(j);
                System.out.print(cell.getRichStringCellValue().getString());
                if (j < list.size() - 1) {
                    System.out.print(", ");
                }
            }
            System.out.println("");
        }
    }
}

请帮忙。 感谢期待, 问候, Dheeraj!

4 个答案:

答案 0 :(得分:36)

您的类路径需要xmlbeans

NoClassDefFoundError表示:

  

当编译当前正在执行的类时,搜索的类定义存在,但无法再找到该定义。

因此,下次遇到这样的异常时,这意味着某些第三方库需要另一个第三方库。然后使用谷歌(或任何其他方式)来查找这是哪个库。 此外,大多数图书馆在其文档和/或发行版中明确说明了它们的依赖性。

答案 1 :(得分:6)

JarFinder建议使用XMLBeans.jar

答案 2 :(得分:1)

在Apache POI 3.16上出现相同的错误。从Apache POI /ooxml-lib/xmlbeans-2.6.0添加了以下jar,以及有关集合/lib/commons-collections4-4.1.jar的下一个异常修复。

答案 3 :(得分:0)

我在 linux env 中也遇到过类似的情况,基本上我的 lib 路径级别降低了 1。