-
Enhancement
-
Resolution: Unresolved
-
P5
-
None
-
None
-
generic
-
generic
A DESCRIPTION OF THE REQUEST :
Currently if you define a project's resource like
Path resource = Paths.get(getClass().getClassLoader().getResource("foo/resource").toUri())
you face a problem when using Files utility class form within the jar.
The Filesystem provider (in this case ZipFileSystem) is not registered by default, which means the whole path interface is useless and not useful at all, since Files can only be used on certain kinds of Path, but it only works by default with the File implementation of Path.
JUSTIFICATION :
Java world would benefit if we could use Path as the new abstraction for pointers to resources, not only File.
One of the things that would get rid of the need for ClassPathResource, ResourceUtils and such, provided by 3rd party fframeworks, would be allowing temporary autoregister of FileSystems.
For this reason alone, we need to keep File as the type for all our internal method signatures, which means we need to copy other kinds of Paths to a temporary ile before.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I dream of a world where defining a main/src/resource is as easy as
Path projectResource =
Paths.get(getClass().getClassLoader().getResource("foo/resource").toUri())
public void methodWithBusinessLogic(Path abstractedResource){
Files.readLines(projectResosurce).forEach(...)
}
The major problem I see currently is supporting things like Files.inputStream, since we need to close the loaded FS after. Well theres always the chance to bind the close() with the one in inputstream.
ACTUAL -
It throws a
Exception in thread "main" java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
---------- BEGIN SOURCE ----------
As it only fails from within a jar, just package a jar with a resource in src/main/resources and then put that in a main
Path resource = Paths.get(getClass().getClassLoader().getResource("myFile").toUri())
Files.readAllLines(resource),
---------- END SOURCE ----------
Currently if you define a project's resource like
Path resource = Paths.get(getClass().getClassLoader().getResource("foo/resource").toUri())
you face a problem when using Files utility class form within the jar.
The Filesystem provider (in this case ZipFileSystem) is not registered by default, which means the whole path interface is useless and not useful at all, since Files can only be used on certain kinds of Path, but it only works by default with the File implementation of Path.
JUSTIFICATION :
Java world would benefit if we could use Path as the new abstraction for pointers to resources, not only File.
One of the things that would get rid of the need for ClassPathResource, ResourceUtils and such, provided by 3rd party fframeworks, would be allowing temporary autoregister of FileSystems.
For this reason alone, we need to keep File as the type for all our internal method signatures, which means we need to copy other kinds of Paths to a temporary ile before.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I dream of a world where defining a main/src/resource is as easy as
Path projectResource =
Paths.get(getClass().getClassLoader().getResource("foo/resource").toUri())
public void methodWithBusinessLogic(Path abstractedResource){
Files.readLines(projectResosurce).forEach(...)
}
The major problem I see currently is supporting things like Files.inputStream, since we need to close the loaded FS after. Well theres always the chance to bind the close() with the one in inputstream.
ACTUAL -
It throws a
Exception in thread "main" java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
---------- BEGIN SOURCE ----------
As it only fails from within a jar, just package a jar with a resource in src/main/resources and then put that in a main
Path resource = Paths.get(getClass().getClassLoader().getResource("myFile").toUri())
Files.readAllLines(resource),
---------- END SOURCE ----------