gpt4 book ai didi

java - 使用 POI 读取 Excel 文件时如何修复 java.lang.ExceptionInInitializerError?

转载 作者:行者123 更新时间:2023-12-04 19:48:56 24 4
gpt4 key购买 nike

显示 java 和包版本信息:

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相关:
org.apache.poi:poi and org.apache.poi:poi-ooxml are all 4.1.2.
读取 test.xlsx 中的数据使用 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();
}
}
}
编译它没有任何问题:
javac  ReadExcel.java
现在运行它:
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
org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory存在。
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
那怎么解决呢?

最佳答案

schemaorg_apache_xmlbeans.system.sD023D6490046BA0250A839A9AD24C443.TypeSystemHolder类包含在 poi-ooxml-schemas-4.1.2.jar 中。所以你需要把那个 jar 放在类路径中。
从您在问题中所说的来看,您似乎在/home/debian/workspace 目录中手动提取了 poi-ooxml-schemas-4.1.2.jar 的某些部分。不要那样做。保留 jar 并将它们放在类路径中。

关于java - 使用 POI 读取 Excel 文件时如何修复 java.lang.ExceptionInInitializerError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69835455/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com