4

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.
Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
KDjava
  • 2,355
  • 4
  • 17
  • 22
  • Seems like a bug in the POI libraries if the error is accurate (enabling core dumps would be useful). Why not just submit a bug report to the project? They will know better how to debug it. – Voo Jul 05 '12 at 12:28
  • You could try to update libzip through your package manager. – Marko Topolnik Jul 05 '12 at 12:44
  • 1
    try Java 7 update 5 and making sure you have plenty of free space on you drive. – Peter Lawrey Jul 05 '12 at 12:52
  • Apache POI is pure Java, so should never be able to trigger a JVM crash. This looks like a bug in the JVM itself, you'll need to report that to Oracle – Gagravarr Jul 05 '12 at 12:54
  • I tried using Java 6 and Java7_05 but the result is same – KDjava Jul 05 '12 at 16:07
  • 1
    Time to report a bug to Oracle then. No Java program should ever be able to crash the JVM, so you've found a JVM bug – Gagravarr Jul 06 '12 at 09:47

3 Answers3

10

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.

Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
1

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));
Scott Wiedemann
  • 335
  • 3
  • 10
0

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

Mark
  • 16,772
  • 9
  • 42
  • 55
  • [Don't use a Stream if you have a File! It uses more memory](http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream), as explained in the same page you linked to... – Gagravarr Feb 25 '16 at 19:10
  • 1
    @Gagravarr Thank you for pointing that out. If I don't use the `InputStream` the JVM crashes. The documentation is just making you aware that it uses more memory, not that it shouldn't be used. – Mark Feb 26 '16 at 15:37