9
votes

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

}

}

12
Can you please post error stack trace ?Rohan
Post your Exception snippetkark
use the search feature. This has been asked hundreds if not thousands of times.Woot4Moo

12 Answers

14
votes

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.

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.

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.

5
votes

I think you forget to checked the library in Project's Property.

  1. Right Click of your project --> Select Property
  2. Select Java Build Path
  3. Select tab Object and Export
  4. And then select your library which you added.
  5. And Ok then run your project again.
4
votes

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

  1. Right click on the project
  2. Buildpath>ConfigureBuildpath
  3. Libraries tab
  4. Add external jar files
1
votes

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!

1
votes

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:

Image shows all necessary libraries

0
votes

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

0
votes

Generally NoClassDefFoundError exception will occur , when the required jar is not available,

Check the below conditions

  1. Check the Jar file path by default you added poi-ooxml-3.9-20121203.jar
  2. Check completely if any additional jar is required, at compile time the program may need any class files which you have not imported

  3. poi-3.7-jdk1.4-20110508-rc2 jar is required .

0
votes

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.

Oracle docs

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

  1. apache
  2. get 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

0
votes

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.

0
votes

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.

0
votes

add xmlbeans-2.3.0.jar in your build path.It is required for xlsx.

0
votes

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:

  1. Manually add libraries below to you class path:

    poi-ooxml-full-5.2.0.jar

    poi-ooxml-schemas-3.15.0.jar

  2. add 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.