'Apache poi excel writer works in ide but not in fat jar? java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML str

The code works perfectly fine in IDE but not in normal .jar or in fat .jar? The program works perfectly fine otherwise. Also non-fat .jar doesn't work either.

The error is:

Jun 08, 2021 11:43:14 AM DataManagers.ExcelWriter$WriterThread run
SEVERE: null
java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML stream or you haven't provide the poi-ooxml*.jar in the classpath/modulepath - FileMagic: OOXML, having providers: [org.apache.poi.hssf.usermodel.HSSFWorkbookFactory@405294c8]
        at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:300)
        at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:199)
        at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:163)
        at DataManagers.ExcelWriter$WriterThread.run(ExcelWriter.java:61)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
        at java.base/java.lang.Thread.run(Thread.java:835)

and this error repeats 3 times.

And my maven pom is:

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Implementation</groupId>
    <artifactId>Implementation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>

            <groupId>org.openjfx</groupId>

            <artifactId>javafx-controls</artifactId>

            <version>11.0.2</version>

        </dependency>
        <dependency>
            <groupId>com.github.mmarquee</groupId>
            <artifactId>ui-automation</artifactId>
            <version>0.4.3</version>
            <type>jar</type>
        </dependency>  
        
       

        <dependency> <!-- Excel -->
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>   <!-- End of excel -->
        
        <dependency>
            <groupId>io.github.cdimascio</groupId>
            <artifactId>java-dotenv</artifactId>
            <version>5.1.1</version>    
        </dependency>   
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.15.1
            </version>
        </dependency>
        
        <dependency>
            <groupId>net.sf.saxon</groupId>
            <artifactId>saxon-dom</artifactId>
            <version>8.7</version>
        </dependency>
        
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>13</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <!-- get all project dependencies -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                        <manifest>
                            <mainClass>UserInterface.Main</mainClass>
                        </manifest>
                    </archive>

                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!-- bind to the packaging phase -->
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.3</version>
                <configuration>
                    <excludes>
                        <exclude>**/UserInterface/*</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.3</version>
            </plugin>  
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.17</version>
                <configuration>
                    <configLocation>checkstyle.xml</configLocation>
                </configuration>
            </plugin>
             
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>2.5</version>
            </plugin>
                
            <!--            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>xls</nonFilteredFileExtension>
                        <nonFilteredFileExtension>xls</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>-->

            
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>UserInterface.Main</mainClass>
                </configuration>
            </plugin>       

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>UserInterface.Main</mainClass>
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>     
        </plugins>   
    </build>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <name>Implementation</name>
</project>

some sample of the code which writes:

FileInputStream f = new FileInputStream(file);
wb = WorkbookFactory.create(f);
s = wb.getSheet("Sheet1");
initialiseExcel();
f.close();
FileOutputStream fOut = new FileOutputStream(file);
initialiseExcel();
Row row = s.createRow(0);
Cell cell = row.createCell(1);
cell.setCellValue("Pvm");    wb.write(fOut);
fOut.close();
...

I have searched for fixes but everyone seems to be saying fat jar fixes it but i'm running the fat jar and the problem still comes up and I don't really have any clue what to do?

Thanks a lot for anyone who cares to help.



Solution 1:[1]

You can use the dependency-plugin to generate all dependencies in a separate directory before the package phase and then include that in the classpath of the manifest by maven-jar-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>theMainClass_ChangeMe_withPackage</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

Alternatively use ${project.build.directory}/classes/lib as output directory to integrate all jar files into the main jar, but then you will need to add custom classloading code to load the jars. I hope this will help as maven-assembly plugin is not working with this scanerio.

Solution 2:[2]

Here is a solution. Service loader use files in META-INF/services for know available providers. When you build your fat jar, providers file from org.apache.poi:poi and org.apache.poi:poi-ooxml are not merged and only one is in your fat-jar. A simple solution is to add a built-in transformer in maven-shade-plugin which do the merges:

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>

Solution 3:[3]

I had the exact same problem. my Java program could read xlsx files in IDE to import data, but the Jar errored out. from my limited Java knowledge, it seems a class used to read big size Excel file is not packaged by Maven. The simple addition to POM to add the transformer solved the problem.

<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> 

Solution 4:[4]

UPD. Add this code to main function of your programm. Solution from: https://github.com/Mararsh/MyBox/issues/1101

WorkbookFactory.addProvider(new HSSFWorkbookFactory());
WorkbookFactory.addProvider(new XSSFWorkbookFactory());

Old post (don't read):

I could not solve this problem on POI 5.0.0 and above, the only working solution is to use the 4.1.2 dependencies.

       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

The solution from @Ankur Mahajan, unfortunately, also doesn't work for some OLE2 files.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ankur Mahajan
Solution 2 xavier-b
Solution 3 wxm146
Solution 4