Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemNotFoundException in a Dockerized Spring Boot Application

I'm trying to load a file in a spring boot application that is running inside a Docker container but I'm getting the following exception:

java.nio.file.FileSystemNotFoundException 
 at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171) ~[zipfs.jar:1.8.0_191] 
   at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157) ~[zipfs.jar:1.8.0_191] 
 at java.nio.file.Paths.get(Paths.java:143) ~[?:1.8.0_191] 
 at app.metrics.collector.util.FileUtils.getContentAsSingleLine(FileUtils.java:17) ~[classes!/:?] 
     at app.metrics.collector.jobs.DbQueryJob.generatePreparedStatement(DbQueryJob.java:54) ~[classes!/:?] 
at app.metrics.collector.jobs.DbQueryJob.execute(DbQueryJob.java:36) ~[classes!/:?] 
  at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [quartz-2.3.0.jar!/:?] 

The error does not appears when I execute the jar outside the container.

Here is the Dockerfile:

FROM openjdk:8-jdk-alpine
ADD build/app.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

And here is the method that loads the file:

    public static String getContentAsSingleLine(String fileName) throws URISyntaxException, IOException {
    String data;
    Path path = Paths.get(FileUtils.class.getClassLoader().getResource(fileName).toURI());
    Stream<String> lines = Files.lines(path);
    data = lines.collect(Collectors.joining(StringUtils.SPACE));
    lines.close();
    return data;
}

The file is located in the resource folder:

/src/main/resources/database/file.sql

and the argument passed to the method is:

"database/file.sql"
  • Does anyone have an idea of the cause of the issue and its possible solution ?
like image 957
andres.enix Avatar asked Aug 03 '26 19:08

andres.enix


1 Answers

I think by execute the jar outside the container you mean running the project in your IDE. Then database/file.sql is actually available as a file.

In the jar, the file is not available for Paths.get() to actually handle.

When trying to access the resource you should use

InputStream in = getClass().getResourceAsStream(fileName);

you can then use java.util.Scanner to collect the lines:

String data = new Scanner(in)
 .useDelimiter('\n')
 .tokens()
 .collect(Collectors.joining(StringUtils.SPACE));
like image 51
Ozan Avatar answered Aug 05 '26 08:08

Ozan