showkey
showkey

Reputation: 320

How can I fix the java.lang.ExceptionInInitializerError when reading an Excel file with POI?

Show the java and package version info:

java  --version
openjdk 11.0.12 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2)
OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2, mixed mode, sharing)

POI related:

org.apache.poi:poi and org.apache.poi:poi-ooxml are all 4.1.2.

Read data in test.xlsx with ReadExcel.java.

cat ReadExcel.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
 
public class ReadExcel
{
    public static void main( String args[] ) throws IOException
    {
        FileInputStream fis = new FileInputStream(new File( "test.xlsx" ) );
        XSSFWorkbook wb = new XSSFWorkbook( fis );
        XSSFSheet sheet = wb.getSheetAt( 0 );
        FormulaEvaluator formulaEvaluator = wb.getCreationHelper()
                .createFormulaEvaluator();
        for( Row row : sheet )     
        {
            for( Cell cell : row )    
            {
                switch( formulaEvaluator.evaluateInCell( cell ).getCellType() )
                {
                case NUMERIC:  
                    System.out.print( cell.getNumericCellValue() + "\t\t" );
                    break;
                case STRING:   
                    System.out.print( cell.getStringCellValue() + "\t\t" );
                    break;
                }
            }
            System.out.println();
        }
    }
}

Compile it without any issue:

javac  ReadExcel.java

Now run it:

java  ReadExcel
Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:86)
    at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
    at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:684)
    at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:288)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:309)
    at ReadExcel.main(ReadExcel.java:17)
Caused by: java.lang.RuntimeException: Cannot load SchemaTypeSystem. Unable to load class with name schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder. Make sure the generated binary files are on the classpath.
    at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:788)
    at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument.<clinit>(Unknown Source)
    ... 8 more
Caused by: java.lang.ClassNotFoundException: schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:774)
    ... 9 more

The org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory exists.

echo  $CLASSPATH
.:/usr/lib/jvm/java-11-openjdk-amd64/lib
 :/usr/share/gdb/auto-load/usr/lib/jvm/java-11-openjdk-amd64/jre/lib
 :/home/debian/workspace:/home/debian/.m2/repository
cd  /home/debian/workspace
ls -al  org/openxmlformats/schemas/drawingml/x2006/main/ThemeDocument$Factory.class
-rw-r--r-- 1 debian debian 1096 Feb  3  2020 org/openxmlformats/schemas/drawingml/x2006/main/ThemeDocument.class

How can fix it then?

Upvotes: 4

Views: 946

Answers (1)

Olivier
Olivier

Reputation: 18260

The schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder class is contained in poi-ooxml-schemas-4.1.2.jar. So you need to put that jar in the classpath.

From what you said in the question, it seems like you manually extracted some part of poi-ooxml-schemas-4.1.2.jar in the /home/debian/workspace directory. Don't do that. Keep the jars and put them in the classpath.

Upvotes: 2

Related Questions