什么给java.lang.NoClassDefFoundError? [英] what give java.lang.NoClassDefFoundError?

查看:30
本文介绍了什么给java.lang.NoClassDefFoundError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取excel文件但给

I want to read excel file but give

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
 at ExcelReader.main(ExcelReader.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
... 1 more

请帮帮我.首先打开 .xlsx 文件,然后给出第一张纸.最后在控制台上打印excel文件的数据.Ps:我在我的项目中添加了 poi-ooxml-3.9-20121203.jar.

please help me. At first open .xlsx file and then give the first sheet. at the end print data of excel file on console. Ps : I add poi-ooxml-3.9-20121203.jar to my project.

    import java.io.File;
    import java.io.FileInputStream;
    import javax.swing.text.html.HTMLDocument.Iterator;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import java.util.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    /**
     * @author mohammad hosein
    *
    */
    public class ExcelReader {

/**
 * @param args
 */
public static void main(String[] args) {
    try
    {
    FileInputStream file = new FileInputStream(new File("E:\\test.xlsx"));

    //Get the workbook instance for XLS file 
    XSSFWorkbook workbook = new XSSFWorkbook (file);

    //Get first sheet from the workbook
    XSSFSheet sheet = workbook.getSheetAt(0);

    //Get iterator to all the rows in current sheet
    java.util.Iterator<Row> rowIterator = sheet.iterator();

    while(rowIterator.hasNext())
    {
        Row row = rowIterator.next();
        java.util.Iterator<Cell> cellIterator = row.cellIterator();

        while(cellIterator.hasNext())
        {
            Cell cell = cellIterator.next();
            System.out.print(cell.getStringCellValue() + "\t");
        }
        System.out.println("");
    }
    }
    catch(Exception e)
    {
        System.out.println("EROR!");
    }

    //Get iterator to all cells of current row

}

}

推荐答案

您的代码无关紧要.NoClassDefFoundError 在编译时可用的类在运行时不可用时发生.如果您提供了完整的堆栈跟踪以及尚未找到的类的实际名称,则可以提供更准确的建议.

Your code is irrelevant. NoClassDefFoundError happens when a class which was available at compilation time is unavailable at runtime. If you provided a full stacktrace, together with the actual name of the class which has not been found, more precise advice could be given.

通常,当您使用与用于构建代码的 JAR 版本不同的 JAR 版本运行代码时,会发生这种情况.恶意 JAR 可能来自应用程序容器或类似容器,并且比正确的 JAR 更早地放置在类路径中.

Typically this happens when you are running your code with a different version of a JAR from the one used to build the code. A rogue JAR may come in from an application container or similar, and be placed earlier on the classpath than your proper JAR.

鉴于您添加的堆栈跟踪,您缺少 Apache POI 的传递依赖项:XMLBeans.您可能在运行时缺少这个 JAR.这完全取决于您运行项目的准确程度.

Given the stacktrace you have added, you are lacking a transitive dependency of Apache POI: XMLBeans. You may be missing this JAR at runtime. This all depends on how exactly you are running your project.

这篇关于什么给java.lang.NoClassDefFoundError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆