'what give java.lang.NoClassDefFoundError?
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
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
}
}
Solution 1:[1]
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.
Solution 2:[2]
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
- Right click on the project
- Buildpath>ConfigureBuildpath
- Libraries tab
- Add external jar files
Solution 3:[3]
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 page for the full details of what everything needs.
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.
If all of that manual stuff is a bit hard for you, use something like Apache Maven or Apache Ivy to manage your dependencies for you.
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!
Solution 4:[4]
Make sure you've "commons-collections.jar" file added in your library otherwise download all libraries file from here:
http://www-us.apache.org/dist/poi/release/bin/poi-bin-3.16-20170419.zip
After download in zip file open "lib" folder you'll find common-collectins.jar file that need to be add in your project library like you can see in attached image:
Solution 5:[5]
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:
This site gives you all the reasons on why you get this error 3 ways on how to resolve it
Solution 6:[6]
Generally NoClassDefFoundError exception will occur , when the required jar is not available,
Check the below conditions
- Check the
Jar file pathby 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-rc2jar is required .
Solution 7:[7]
As the name suggests ClassNotFoundException in Java is a subclass of java.lang.Exception and Comes when Java Virtual Machine tries to load a particular class and doesn't found the requested class in classpath.
Another important point about this Exception is that, It is a checked Exception and 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.
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.
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
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
Solution 8:[8]
There are 2 important jar files that come along with poi-3.9 which needs to be included as XSSFWorkbook use them
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.
Solution 9:[9]
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.
Solution 10:[10]
add xmlbeans-2.3.0.jar in your build path.It is required for xlsx.
Solution 11:[11]
As far as i could check any reviews about this problem, I reached to the steps to make my code run without any "java.lang.NoClassDefFoundError" error:
Manually add libraries below to you class path:
poi-ooxml-full-5.2.0.jarpoi-ooxml-schemas-3.15.0.jaradd dependencies to you
pom.xml:poi (5.1.0) from (org.apache.poi)poi-ooxml (5.1.0) from (org.apache.poi)log4j-core (2.16.0) from (org.apache.logging.log4j)log4j-api (2.17.1) from (org.apache.logging.log4j)
I included versions that are working correctly for me under Intellij IDEA 2021.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow

