0

i'm trying for the first time the apache poi library to manage Execel files. I follow this steps to import apache poi: Import Apache POI in Intellij for JAVA. The code compile without error but when i run it give me this error messages:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject at Test.main(Test.java:16) Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519) ... 1 more

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {
    public static void main(String[] args) {
        try {
            File file = new File("C:\\Users\\Huawei\\IdeaProjects\\untitled\\src\\TEST.xlsx");
            FileInputStream fis = new FileInputStream(file);
            XSSFWorkbook wb = new XSSFWorkbook(fis);
            XSSFSheet sheet = wb.getSheetAt(0);
            Iterator<Row> itr = sheet.iterator();
            while(itr.hasNext()){
                Row row = itr.next();
                Iterator<Cell> celliterator = row.cellIterator();
                while(celliterator.hasNext()){
                    Cell cell = celliterator.next();
                    switch (cell.getCellType()){
                        case STRING:    //field that represents string cell type
                            System.out.print(cell.getStringCellValue() + "\t\t\t");
                            break;
                        case NUMERIC:    //field that represents number cell type
                            System.out.print(cell.getNumericCellValue() + "\t\t\t");
                            break;
                        default:
                            System.out.println("NOT NUMERIC AND NOT STRING");
                    }
                }
            }

        }catch (Exception e ){
            e.printStackTrace();
        }

        }
    }
  • 1
    Do yourself a huge favor and use Maven or Gradle - you will save loads of time and do not have to chase dependencies and dependencies of dependencies and dependencies of dependencies of dependencies to get your project to work... – mikeb Sep 25 '21 at 21:13

1 Answers1

1

This seems like a classic Maven Project with a .jar downloaded not installed via mvn command.

If ClassNotFoundException raises in runtime, its because your project can't find the library after compiled.

Is this the case? If so, you must add the .jar via mvn install and declare him in your pom.xml. A better solution it's just adding the maven dependency directly:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>