I am getting FileNotFoundException while the file is clearly present in the jar. why is it so?
java.io.FileNotFoundException: file:/Users/serviceuser/project/coolApp/target/coolApp-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/ssl_certs/mysslstore.jks (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method) ~[na:na]
at java.base/java.io.FileInputStream.open(FileInputStream.java:219) ~[na:na]
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) ~[na:na]
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112) ~[na:na]
However, I see the files packaged in jar.
jar -tf coolApp-1.0-SNAPSHOT.jar | grep ssl
EDIT I load the file as following
new FileInputStream(CoolApp.class.getClassLoader().getResource("ssl_certs/mysslstore.jks").getFile())
Here :
new FileInputStream(CoolApp.class.getClassLoader().getResource("ssl_certs/mysslstore.jks").getFile());
getFile()
is invoked on a URL
included in a jar.
As a result, it provides a particular File
object since that is not a File
directly accessible in the filesystem.
And the URL
javadoc confirms that (emphasis is mine) :
Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine.
So the FileInputStream(File)
constructor cannot necessarily be able to open that "special" file :
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
You can compare what you try to do with the following :
new FileInputStream("/Users/serviceuser/project/coolApp/target/coolApp-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/ssl_certs/mysslstore.jks")
As you guessed, the file included in the jar(mysslstore.jks
) could not be resolved by the OS filesystem.
Instead of, use getResourceAsStream()
that returns an input stream. That input stream refers to the bytes sequence represented by the resource. In this way, the client code doesn't depend any longer on the way which the resource is stored.
InputStream is = CoolApp.class.getClassLoader().getResourceAsStream("ssl_certs/mysslstore.jks"));