什么给java.lang.NoClassDefFoundError?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18916350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what give java.lang.NoClassDefFoundError?
提问by sharafi
I want to read excel file but give
我想阅读excel文件,但给
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.run(Unknown Source)
at java.net.URLClassLoader.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
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.
请帮我。首先打开 .xlsx 文件,然后给出第一张纸。最后在控制台上打印excel文件的数据。Ps:我将 poi-ooxml-3.9-20121203.jar 添加到我的项目中。
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
}
}
}
采纳答案by Marko Topolnik
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.
您的代码无关紧要。NoClassDefFoundError
当编译时可用的类在运行时不可用时发生。如果您提供了完整的堆栈跟踪以及尚未找到的类的实际名称,则可以提供更精确的建议。
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.
通常,当您使用与用于构建代码的 JAR 版本不同的 JAR 版本运行代码时,会发生这种情况。流氓 JAR 可能来自应用程序容器或类似容器,并且比正确的 JAR 更早地放置在类路径中。
Update
更新
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.
鉴于您添加的堆栈跟踪,您缺少 Apache POI 的传递依赖项:XMLBeans。您可能在运行时缺少这个 JAR。这一切都取决于您如何准确地运行您的项目。
回答by Arun
NoClassDefFoundError
will come if a class was present during compile time but not available in java classpath during runtime. Normally you will see below line in log when you get NoClassDefFoundError:
NoClassDefFoundError
如果类在编译时存在但在运行时在 java 类路径中不可用,则会出现。通常,当您收到 NoClassDefFoundError 时,您会在日志中看到以下行:
This sitegives you all the reasons on why you get this error 3 ways on how to resolve it
该站点为您提供了出现此错误的所有原因 关于如何解决此错误的 3 种方法
回答by System Integration Pvt Ltd
I think you forget to checked the library in Project's Property.
我认为您忘记检查项目属性中的库。
- Right Click of your project --> Select Property
- Select Java Build Path
- Select tab Object and Export
- And then select your library which you added.
- And Ok then run your project again.
- 右键单击您的项目 --> 选择属性
- 选择 Java 构建路径
- 选择选项卡对象并导出
- 然后选择您添加的库。
- 好吧,然后再次运行你的项目。
回答by Aniket Kulkarni
As the name suggests ClassNotFoundException
in Java is a subclass of java.lang.Exception
and Comes when Java Virtual Machinetries to load a particular class and doesn't found the requested class in classpath.
作为顾名思义ClassNotFoundException
是用Java的一个子类java.lang.Exception
,当谈到Java虚拟机试图加载特定的类和没有发现的请求的类的类路径。
Another important point about this Exception is that, It is a checked Exceptionand you need to provide explicitly Exception handling while using methods which can possibly throw ClassNotFoundException
in java either by using try-catch block or by using throws clause.
关于此异常的另一个重要点是,它是一个已检查的异常,您需要在使用可能ClassNotFoundException
通过 try-catch 块或使用 throws 子句在 java 中抛出的方法时提供显式异常处理。
public class ClassNotFoundException
extends ReflectiveOperationException
Thrown when an application tries to load in a class through its string name using:
当应用程序尝试通过其字符串名称加载类时抛出:
- The forName method in class Class.
- The findSystemClass method in class ClassLoader .
- The loadClass method in class ClassLoader.
- Class 类中的 forName 方法。
- ClassLoader 类中的 findSystemClass 方法。
- ClassLoader 类中的 loadClass 方法。
but no definition for the class with the specified name could be found.
但是找不到具有指定名称的类的定义。
Add the poi-ooxml-schemas-3.9-20121203.jar
file to class path to avoid exception.
You will find on these links
将poi-ooxml-schemas-3.9-20121203.jar
文件添加到类路径以避免异常。
你会在这些链接上找到
Edit
编辑
You're missing the extra jar files that come with POI. Include them in your class path.
you need to include a jar file named xmlbeans-x.x.x.jar
get jar here.
XMLBeans
您缺少 POI 附带的额外 jar 文件。将它们包含在您的类路径中。
您需要在xmlbeans-x.x.x.jar
此处包含一个名为get jar的 jar 文件。
XMLBeans
回答by kark
Generally NoClassDefFoundError
exception
will occur , when the required jar is not available,
一般 NoClassDefFoundError
exception
会发生,当需要的jar不可用时,
Check the below conditions
检查以下条件
- Check the
Jar file path
by default you addedpoi-ooxml-3.9-20121203.jar
Check completely if any additional jar is required, at compile time the program may need any class files which you have not imported
poi-3.7-jdk1.4-20110508-rc2
jar is required .
- 检查
Jar file path
默认情况下您添加的poi-ooxml-3.9-20121203.jar
完全检查是否需要任何额外的 jar,在编译时程序可能需要您尚未导入的任何类文件
poi-3.7-jdk1.4-20110508-rc2
jar 是必需的。
回答by Gagravarr
The Apache POI documentation provides a full list of the different components and their dependencies. You state that you want to use poi-ooxml
but you seem to have missed off the xmlbeans
dependency (and maybe others too!). See the components pagefor the full details of what everything needs.
Apache POI 文档提供了不同组件及其依赖项的完整列表。您声明要使用,poi-ooxml
但您似乎错过了xmlbeans
依赖项(可能还有其他人!)。请参阅组件页面以获取所有需要的完整详细信息。
If you download a binary release of Apache POI, then you'll find all of your dependencies you might need handily contained with the package. Just add the ones in you need.
如果您下载Apache POI的二进制版本,那么您会发现您可能需要的所有依赖项都包含在该包中。只需添加您需要的那些。
If all of that manual stuff is a bit hard for you, use something like Apache Mavenor Apache Ivyto manage your dependencies for you.
如果所有这些手动操作对您来说都有些困难,请使用Apache Maven或Apache Ivy 之类的工具来为您管理依赖项。
Next up, you need all those jars available twice. Once for compiling, once for running. Just having the jars in eclipse or similar may not be enough, you also need to get them into your production environment!
接下来,您需要两次使用所有这些罐子。一次编译,一次运行。仅将 jars 放在 eclipse 或类似的地方可能还不够,您还需要将它们放入您的生产环境中!
回答by vikas kapdoskar
There are 2 important jar files that come along with poi-3.9 which needs to be included as XSSFWorkbook use them
poi-3.9 附带了 2 个重要的 jar 文件,需要包含在 XSSFWorkbook 中使用它们
xmlbeans*.jar dom4j-*.jar
xmlbeans*.jar dom4j-*.jar
Both these jars are present in the ooxml-lib folder and is a part of poi-3.9*.zip. Include them in your Library and that should solve the problem.
这两个 jar 都存在于 ooxml-lib 文件夹中,并且是 poi-3.9*.zip 的一部分。将它们包含在您的库中,这应该可以解决问题。
回答by RudebwoyG
I just had the same problem. I had added the JARs in \ooxml-lib\ folder by adding the whole folder to the Libraries (using Netbeans). I removed this and added each one manually, and it worked.
我只是遇到了同样的问题。我通过将整个文件夹添加到库中(使用 Netbeans)在 \ooxml-lib\ 文件夹中添加了 JAR。我删除了它并手动添加了每个,并且它起作用了。
回答by Saurabh
add xmlbeans-2.3.0.jar in your build path.It is required for xlsx.
在你的构建路径中添加 xmlbeans-2.3.0.jar。它是 xlsx 所必需的。
回答by user3437379
This will happen when all the required poi jar files are not added.So my suggestion is add all the required jar files. Add the jar files inside lib and ooxml-lib folders along with the poi-3.15-beta2 jar files. How to Add jar
当没有添加所有必需的 poi jar 文件时,就会发生这种情况。所以我的建议是添加所有必需的 jar 文件。添加 lib 和 ooxml-lib 文件夹中的 jar 文件以及 poi-3.15-beta2 jar 文件。如何添加罐子
- Right click on the project
- Buildpath>ConfigureBuildpath
- Libraries tab
- Add external jar files
- 右键单击项目
- 构建路径>配置构建路径
- 库选项卡
- 添加外部jar文件