I have a Maven project and inside a method I want to create a path for a directory in my resources folder. This is done like this:
try {
final URI uri = getClass().getResource("/my-folder").toURI();
Path myFolderPath = Paths.get(uri);
} catch (final URISyntaxException e) {
...
}
The generated URI
looks like jar:file:/C:/path/to/my/project.jar!/my-folder
.
The stacktrace is as following:
Exception in thread "pool-4-thread-1" java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Paths.java:143)
The URI
seems to be valid. The part before !
points to the generated jar-file and the part after it to my-folder
in the root of the archive. I have used this instructions before to create paths to my resources. Why am I getting an exception now?
URL
, you don't need aFile
orPath
just to read its contents. You can just callURL.openStream()
and read from theInputStream
that method returns. If you actually must have aFile
orPath
object, then you'll need theFileSystem
fixes mentioned below. But most things don't really need a file and can handleInputStream
orReader
interfaces, so I'd suggest going down theURL.openStream()
route first if possible.