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

时间:2022-05-04 15:36:07

I have to read xls file in java.I used poi-3.6 to read xls file in Eclipse.But i m getting this ERROR"Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException at ReadExcel2.main(ReadExcel2.java:38)".

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

I have added following jars 1)poi-3.6-20091214.jar 2)poi-contrib-3.6-20091214.jar 3)poi-examples-3.6-20091214.jar 4)poi-ooxml-3.6-20091214.jar 5)poi-ooxml-schemas-3.6-20091214.jar 6)poi-scratchpad-3.6-20091214.jar

我添加了以下罐子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

Below is the code which i m using:

下面是我正在使用的代码:

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("");
        }
    }
}

Please help. thanks in anticipation, Regards, Dheeraj!

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

3 个解决方案

#1


34  

You need xmlbeans on your classpath.

类路径中需要xmlbeans。

NoClassDefFoundError means that:

NoClassDefFoundError意味着:

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

当当前正在执行的类被编译时,存在搜索类定义,但是不能再找到定义。

So next time you get an exception like this, it means that some 3rd party library requires another 3rd party library. Then use google (or any other means) to find which library this is. Furthermore, most libraries state clearly in their documentations and/or distributions what are their dependencies.

所以下次你遇到这样的异常时,就意味着某个第三方库需要另一个第三方库。然后使用谷歌(或任何其他方法)查找这是哪个库。此外,大多数库在文档和/或发行版中清楚地声明它们的依赖项。

#2


6  

JarFinder suggests XMLBeans.jar

JarFinder表明XMLBeans.jar

#3


1  

Had the same error on Apache POI 3.16. Added the following jars from Apache POI /ooxml-lib/xmlbeans-2.6.0 and for the next exception regarding collections /lib/commons-collections4-4.1.jar to fix.

在Apache POI 3.16上有相同的错误。在Apache POI /ooxml-lib/xmlbeans-2.6.0中添加了以下jar,并为下一个关于集合/lib/commons-collections4-4.1的异常添加了。jar来解决。

#1


34  

You need xmlbeans on your classpath.

类路径中需要xmlbeans。

NoClassDefFoundError means that:

NoClassDefFoundError意味着:

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

当当前正在执行的类被编译时,存在搜索类定义,但是不能再找到定义。

So next time you get an exception like this, it means that some 3rd party library requires another 3rd party library. Then use google (or any other means) to find which library this is. Furthermore, most libraries state clearly in their documentations and/or distributions what are their dependencies.

所以下次你遇到这样的异常时,就意味着某个第三方库需要另一个第三方库。然后使用谷歌(或任何其他方法)查找这是哪个库。此外,大多数库在文档和/或发行版中清楚地声明它们的依赖项。

#2


6  

JarFinder suggests XMLBeans.jar

JarFinder表明XMLBeans.jar

#3


1  

Had the same error on Apache POI 3.16. Added the following jars from Apache POI /ooxml-lib/xmlbeans-2.6.0 and for the next exception regarding collections /lib/commons-collections4-4.1.jar to fix.

在Apache POI 3.16上有相同的错误。在Apache POI /ooxml-lib/xmlbeans-2.6.0中添加了以下jar,并为下一个关于集合/lib/commons-collections4-4.1的异常添加了。jar来解决。