JVM crashing while writing to XLSX file( POI)

南楼画角 提交于 2019-12-06 06:09:53

问题


JVM is crashing while trying to write to the .xlsx file. I am using POI(XSSF) for the same. The error location point in code is the write method--> workBook.write(fileOutputStream);

On Console I get..

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.

回答1:


The solution I've found for this, and I've been looking for a while, is to make sure you don't open your Workbook with the File which you use to open the FileOutputStream to save the Workbook. Instead, use a FileInputStream to open the Workbook.

Something like this will work flawlessly

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

Don't forget to close every opened stream and the OPCPackage.




回答2:


None of the other solutions worked for me. I only needed readonly access to my Excel files, and setting the readonly flags worked for me:

Workbook wb = new XSSFWorkbook(OPCPackage.open(file, PackageAccess.READ));
Workbook wb = new HSSFWorkbook(new POIFSFileSystem(file, true));



回答3:


Using OPCPackage did not fix the JVM crash for me but using WorkbookFactory did. If you look at the POI Busy Developers Guide they provide example of reading and writing the same Excel file.

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

Using Apache POI version 3.13, Java 1.8



来源:https://stackoverflow.com/questions/11344131/jvm-crashing-while-writing-to-xlsx-file-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!