11

Reading a file from the executable JAR file itself using ClassLoader.getResourceAsStream(...) is a known concept to me, but how would I do the same using Java NIO?

The target is to have a function as follows:

static String readFullyFromJar(String filename) {
    final Path path = Paths.get(Main.class.getResource(fileName).toURI());
    final byte[] bytes = Files.readAllBytes(path);
    return new String(bytes, CHARSET_ASCII);
}

While this works fine in the IDE, I get a

java.nio.file.FileSystemNotFoundException
    at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171))

when I try this with the real JAR, even though the target file is in the correct place.

4

1 回答 1

9

事实证明,我上面的代码实际上是正确的,问题在于 Java 本身。

根据这个Bug ID,Java 没有正确使用 ZipFileSystemProvider。应该在 Java 8 中修复。(我的实际问题在此重复报告中描述)

于 2013-10-10T22:51:47.397 回答