On a Spring Boot application (2.3.3), I have a dependency to a module developed by my company. From a service I'm calling a method from this dependency which needs a file loaded from resources (src/main/resources/META-INF/spring-main-cfg.xml
), so I've copied and pasted this file to my Spring Boot application resources.
Here's the code executed in that dependency:
InputStream in = RSAEncrypter.class.getClassLoader().getResourceAsStream(keyFileName);
// StreamCorruptedException here
ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
Stack:
java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:936) ~[na:1.8.0_281]
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:394) ~[na:1.8.0_281]
The thing is, I've created a new dummy Maven project with only this dependency and a main to execute that code and it works.
I don't understand what could be the reasons I've got this exception executed from my Spring Boot application. I've got the same Java version, the file read is the same.
How can I fix this to prevent this exception from occurring?
I've finally found the issue, it was the file it self since I had a global:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
The Maven doc on this say:
Warning: Do not filter files with binary content like images! This will most likely result in corrupt output.
https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
So I just added an exception for my file :
<resources>
<!-- Only properties files have to be filtered.
META-INF/spring-main-cfg.xml must not be or StreamCorruptedException occure by reading it -->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>