brain storm
brain storm

Reputation: 31272

FileNotFoundException in spring boot jar but the file is present

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

enter image description here

EDIT I load the file as following

new FileInputStream(CoolApp.class.getClassLoader().getResource("ssl_certs/mysslstore.jks").getFile())

Upvotes: 6

Views: 16578

Answers (3)

alok
alok

Reputation: 2756

I was facing the same issue in Spring Boot, and I have tried many things, like the ones below, but none of them is working. These all work locally, but it is giving FileNotFoundException in the dev environment because when we build a JAR, the resources get placed into the root directory of the packaged artifacts, and our downloaded files that I placed inside the resource folder.

// Method 1: Working solution for jars
    InputStream stram = getClass().getClassLoader().getResourceAsStream(File.separator + "database.properties");
    URL url = getClass().getClassLoader().getResource(File.separator + "database.properties");
    
// The only thing that worked was ResourceLoader. Need to Autowire it:
        @Autowired
        ResourceLoader resourceLoader;
    
            Resource resource = resourceLoader.getResource("classpath:" + File.separatorChar + NPIConstants.VAR_FOLDER
                    + File.separatorChar + NPIConstants.TPSPIDsBUCKET_FILENAME);
            InputStreamResource isResource = new InputStreamResource(resource.getInputStream());
        
// Method 1: Not Working solution for jars
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    InputStream is = loader.getResourceAsStream("database.properties");
    System.out.println(is.available());
            
// Method 2: Not Working solution for jars
    Resource resource = new ClassPathResource("database.properties");
    InputStream input = resource.getInputStream();
    
// Method 3: Not Working solution for jars
    File file = new File("resources/database.properties");
    String absolutePath = file.getAbsolutePath();
    
// Method 4: Not Working solution for jars
    File file = new File(getClass().getClassLoader().getResource("database.properties").getFile());
    
// Method 5: Not Working solution for jars
    ClassLoader classLoader = getClass().getClassLoader();
    classLoader.getResource("database.properties");
    String path = classLoader.getResource("database.properties").getPath();

Upvotes: 0

davidxxx
davidxxx

Reputation: 131546

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"));

Upvotes: 13

Daisy Day
Daisy Day

Reputation: 688

Files like this need to be in the resources directory.

Upvotes: -4

Related Questions