Java 写入XLSX文件(POI)时JVM崩溃
JVM在尝试写入.xlsx文件时崩溃。我正在使用POI(XSSF)进行同样的操作。 代码中的错误位置点是writeJava 写入XLSX文件(POI)时JVM崩溃,java,crash,jvm,apache-poi,jvm-crash,Java,Crash,Jvm,Apache Poi,Jvm Crash,JVM在尝试写入.xlsx文件时崩溃。我正在使用POI(XSSF)进行同样的操作。 代码中的错误位置点是writemethod-->工作簿.write(fileOutputStream) 在控制台上我得到 A fatal error has been detected by the Java Runtime Environment: SIGBUS (0x7) at pc=0xb68d77f3, pid=14653, tid=1849355120 JRE version: 7.0_04-b
method-->工作簿.write(fileOutputStream)代码>
在控制台上我得到
A fatal error has been detected by the Java Runtime Environment:
SIGBUS (0x7) at pc=0xb68d77f3, pid=14653, tid=1849355120
JRE version: 7.0_04-b20
Java VM: Java HotSpot(TM) Server VM (23.0-b21 mixed mode linux-x86 )
Problematic frame:
C [libzip.so+0x47f3] newEntry+0x73
Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
If you would like to submit a bug report, please visit:
http://bugreport.sun.com/bugreport/crash.jsp
The crash happened outside the Java Virtual Machine in native code.
See problematic frame for where to report the bug.
我已经找到了解决方法,我已经寻找了一段时间,就是确保你没有用文件打开工作簿
,你用它打开文件输出流
来保存工作簿
。相反,请使用文件输入流
打开工作簿
像这样的东西可以完美地工作
File inputFile = new File("Your-Path");
this.inputStream = new FileInputStream(inputFile);
this.opc = OPCPackage.open(this.inputStream);
this.workbook = WorkbookFactory.create(opc);
...
this.outputStream = new FileOutputStream(inputFile);
this.workbook.write(this.outputStream);
别忘了关闭每个打开的流,使用OPCPackage
的OPCPackage并没有为我修复JVM崩溃,但使用WorkbookFactory
修复了。如果您查看,它们提供了读取和写入同一Excel文件的示例
File excelFile = new File("workbook.xlsx");
InputStream inp = new FileInputStream(excelFile);
Workbook wb = WorkbookFactory.create(inp);
FileOutputStream fileOut = new FileOutputStream(excelFile);
wb.write(fileOut);
fileOut.close();
使用ApachePOI版本3.13、Java1.8,其他解决方案都不适合我。我只需要对Excel文件进行只读访问,设置只读标志对我很有效:
Workbook wb = new XSSFWorkbook(OPCPackage.open(file, PackageAccess.READ));
Workbook wb = new HSSFWorkbook(new POIFSFileSystem(file, true));
如果错误是准确的,那么POI库中似乎有一个bug(启用核心转储会很有用)。为什么不向项目提交一份bug报告呢?他们将更好地了解如何调试它。您可以尝试通过包管理器更新libzip。尝试使用Java 7 update 5,并确保驱动器上有足够的可用空间。Apache POI是纯Java,因此永远不会触发JVM崩溃。这看起来像是JVM本身的一个bug,您需要向OracleTime报告,然后再向Oracle报告一个bug。任何Java程序都不应该使JVM崩溃,因此您发现了一个JVM错误,正如您链接到的同一页中所解释的…@Gagravarr感谢您指出这一点。如果我不使用InputStream
,JVM就会崩溃。文档只是让您意识到它使用了更多内存,而不是不应该使用它。