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.
Jar files are archive files that contains of a lot of different java classes (files). You can use winzip/winrar to open the jar files and you can see those java classes in jar files. Typically you can use a Java decompiler to decompile the class file and look into the source code.
JAR files are packaged in the ZIP file format. The unzip command is a commonly used utility for working with ZIP files from the Linux command-line. Thanks to the unzip command, we can view the content of a JAR file without the JDK.
It turns out that my code above is actually correct, the issue is within Java itself.
According to this Bug ID, Java is not properly using the ZipFileSystemProvider as it should. Supposed to be fixed in Java 8. (My actual problem is described in this duplicate report)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With