user3504411
user3504411

Reputation: 51

java - Apache POI SXSSFWorkbook Unable to create sheets

In a much bigger code flow in my project I am trying to create a blank SXSSFWorkbook and create sheets and write data to it but I am unable to as its giving me a Runtime Exception of : No such file or directory. I do not understand why that is happening. I went through the code and could not find any reason.

Jars used:

poi-3.10-FINAL.jar
poi-ooxml-3.10-FINAL.jar

java.lang.RuntimeException: java.io.IOException: No such file or directory at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:568) at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:584)

Code :

csvDataWorkbook = new SXSSFWorkbook(-1);
Sheet sheet = csvDataWorkbook.createSheet("csvDataSheet");

The second line above throws that exception. Might sound weird but this was working fine till yesterday, but has stopped from today.

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <groupId>com.company</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>


    <dependencies>
        .......
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.10-FINAL</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.10-FINAL</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.10-FINAL</version>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.10-FINAL</version>
        </dependency>



    </dependencies>
</project>

Upvotes: 0

Views: 16501

Answers (3)

Ludovic Jutant
Ludovic Jutant

Reputation: 41

If you have several jvm on the same server, I advise you to use version 3.12 and indicate yourself where you want to write :

File generateTempDir = ...
//OR File generateTempDir = new File(System.getProperty("java.io.tmpdir"),"poifiles");
TempFile.setTempFileCreationStrategy(new TempFile.DefaultTempFileCreationStrategy(generateTempDir));

(or set per jvm the java.io.tmpdir property)

Upvotes: 0

D.Shev
D.Shev

Reputation: 371

I have faced with exact problem during running integration tests with diff contexts.

There is a bug in POI.

SXSSF creates a temporary file when its saved using the class org.apache.poi.util.TempFile. When a tempfile is first created, the directory name is saved within this class, in the static singleton DefaultTempFileCreationStrategy object. This directory is only created on the first save and is automatically deleted when the virtual machine exits and its never checked if this directory still exists.

public File createTempFile(String prefix, String suffix) throws IOException {
            // Identify and create our temp dir, if needed
            if (dir == null)
            {
                dir = new File(System.getProperty("java.io.tmpdir"), "poifiles");
                dir.mkdir();
                if (System.getProperty("poi.keep.tmp.files") == null)
                    dir.deleteOnExit();
            }

So, if you have two applications both loading and saving excels and you then close one this directory is automatically deleted. Now both applications used exactly the same temp directory. Therefore if you try to save your file in the second application which is still open, an IOException is instead raised as it tries to create the tmp file in the no-longer existing directory.

For more details check Bug 57200 - SXSSF saving fails sometimes as TempFile creation fails

Possible solutions:

  • Use temp folder "java.io.tmpdir" instead of its sub-folder "poifiles".
  • Use a unique temp folder name for each process instead of "poifiles".
  • Never delete sub-folder "poifiles".
  • Delete folder "poifiles" only if the user explicitely sets a corresponding system property, e.g. "poi.delete.tmp.dirs" (setting this property must also delete the contained temporary files even if "poi.keep.tmp.files" is specified).

I have fixed it by "never delete sub-folder. Create folder and do not use dir.deleteOnExit();

File dir = new File(System.getProperty("java.io.tmpdir"),"poifiles");
dir.mkdir();
TempFile.setTempFileCreationStrategy(new TempFile.DefaultTempFileCreationStrategy(dir));

Upvotes: 5

dayalag
dayalag

Reputation: 11

If you are running it on Apache, check whether the temp folder exist.

Can be found in tomcat7w.exe inside tomcat bin folder.

Refer below image, https://i.sstatic.net/y6VGc.jpg

-Djava.io.tmpdir=

If the option is not available, try creating it.

Upvotes: 1

Related Questions