1

I am using apache-poi-3.16.I am trying to write a file multiple times instead of one shot but getting heal dump error in intelliJ idea.Please find the below code for reference,

public class SampleExcelGeneration {

public static void main(String[] args) {
    SampleExcelGeneration excelGeneration = new SampleExcelGeneration();
    excelGeneration.writeFile();
    excelGeneration.writeFile();
    excelGeneration.writeFile();
    excelGeneration.writeFile();
    excelGeneration.writeFile();

}

private void writeFile () {
    try {
        final File file = new File("test.xlsx");
        XSSFWorkbook wb;
        XSSFSheet sheet;
        if(file.exists()) {
            wb = new XSSFWorkbook(file.getAbsolutePath());
            sheet = wb.getSheetAt(0);
        } else {
            wb = new XSSFWorkbook();
            sheet = wb.createSheet("Results");
        }
        int getLastRow = sheet.getPhysicalNumberOfRows();
        int incrementLastRow = getLastRow+1;
        XSSFRow row = sheet.createRow(incrementLastRow);
        XSSFCell column1 = row.createCell(0);
        column1.setCellValue(1);
        XSSFCell column2 = row.createCell(1);
        column2.setCellValue(3);
        XSSFCell column3 = row.createCell(2);
        column3.setCellValue(380);
        XSSFCell column4 = row.createCell(3);
        column4.setCellValue("sample");
        XSSFCell column5 = row.createCell(4);
        column5.setCellValue("value");
        //write this workbook to an Outputstream.
        wb.write(new FileOutputStream(file.getAbsolutePath()));
        wb.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

While executing getting content in console,

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGBUS (0xa) at pc=0x000000010acbde92, pid=77146, tid=0x0000000000001003
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.101-b13 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# C  [libzip.dylib+0x2e92]  newEntry+0x154
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/dev/sample-excel/hs_err_pid77146.log
Compiled method (nm)    1835  100     n 0       
java.util.zip.ZipFile::getEntry (native)
total in heap  [0x000000010b7322d0,0x000000010b732630] = 864
relocation     [0x000000010b7323f8,0x000000010b732438] = 64
main code      [0x000000010b732440,0x000000010b732630] = 496
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

If any any other exception means i can get a hint to fix the issue but instead it is throwing a heap dump error and it corrupts the file.

The reason can be improper handling of stream but as of my knowledge i closed all the streams.

Any help or hint would be appreciable.

VelNaga
  • 3,593
  • 6
  • 48
  • 82
  • What if you tried `getLastRowNum()` instead? – Joop Eggen Oct 18 '17 at 10:38
  • Try to get the parameters of jvm through jstat https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html – Алексей Мокрев Oct 18 '17 at 10:38
  • @JoopEggen Do you want to use getLastRowNum() instead of getPhysicalNumberOfRows() ? If yes means i have already tried but no luck. – VelNaga Oct 18 '17 at 10:41
  • @АлексейМокрев Sorry i don't understand why do i get JVM params ? – VelNaga Oct 18 '17 at 10:42
  • @VelNaga yes, and I did not really think that would help. What if you do one single writeFile in main. And start the application 5 times? (Data error or memory leak.) Also check after every call the sheet; whether it grows. Paranoia: make sure the file is not opened in Excel. – Joop Eggen Oct 18 '17 at 10:46
  • @JoopEggen Now in my main method i am calling writeFile() only once first time it is success and i can able to open a file but second time getting the same heap dump exception and the file is corrupted. – VelNaga Oct 18 '17 at 10:50
  • Try `new XSSFWorkbook(file);` - I know that for the overload with InputStream is warned. – Joop Eggen Oct 18 '17 at 11:04
  • @JoopEggen no luck the issue remains same.. – VelNaga Oct 18 '17 at 11:27
  • 1
    The `new XSSFWorkbook(file.getAbsolutePath());` opens the underlying `OPCPackage` from a file and this file is open also while `wb.write(new FileOutputStream(file.getAbsolutePath()));`. So you are trying to write using an `OutputStream` into an opened file. See https://stackoverflow.com/questions/46146161/apache-poi-fileinputstream-works-file-object-fails-nullpointerexception/46149469#46149469. – Axel Richter Oct 18 '17 at 12:13

0 Answers0