Home > OS >  can't find apache poi libraries
can't find apache poi libraries

Time:09-26

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();
        }

        }
    }

CodePudding user response:

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>

CodePudding user response:

There can be three ways to make it work:

  1. Using maven(Recommended): Create a maven project and declare the following dependencies:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.1</version>
</dependency>
  1. Importing the libraries into the IDE: Check this answer for IntelliJ.
  2. Specifying the jars as the classpath parameters while running the class file on the command line e.g.
java -cp foo.jar Bar
  • Related