Tech stack details:
Spring Boot version 2.7.0
Java Version : Java 17.0.3.1
Operating System : Windows 10 Pro
Setup: Trying to run the application built using Spring Boot and packaged as JAR
Business code reads a JSON file. Currently it was achieved by opening an InputStream
over BufferedReader readLine()
As part of JDK upgrade from v8 to v17. we are planning to achieve the same using java.nio.file.Files readString(Path, Charset)
. readString takes Path as an argument. We are trying to build the path using Paths.get(new ClassPathResource(WAVEFORM_FILE_NAME).getURI()
But above line is throwing java.nio.file.FileSystemNotFoundException
at ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:156)
. After debug what I understood is ZipFileSystemProvider
has a hashMap filesystems
. This is EMPTY. So even when uriToPath(URI uri) is building Path properly, getFileSystem(URI uri) of ZipFileSystemProvider is throwing FileSystemNotFoundException at line#156.
Stack Trace:
at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:156)
at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:142)
at java.base/java.nio.file.Path.of(Path.java:208)
at java.base/java.nio.file.Paths.get(Paths.java:98)
at com.x.y.z.streaming.mock.DataLoader.loadData(DataLoader.java:42)
Please note: things work fine when spring application is ran inside an editor. Issue comes when we run application as java -jar app.jar
I see suggestions to use FileSystems.newFileSystem(URI.create(pathArray[0]), env)
to make this work. I do not understand why Path.get()
is having a challenge to read a JSON file inside SpringBoot built JAR. Any pointers would help!
I tried to debug and understand why the exception is thrown. Root Cause Analysis is given in the question. Also proposed solution in some discussion thread also tried. It seems to work but do not understand why Path.get() is not able to work independently. Trying to discuss the technicality.